The government must have reasonable suspicion to stop you and ask you questions.
The government must have probable cause to arrest you.
The government cannot question you if you have invoked your 5th Amendment rights
The government must release you if you post bail which is set by a magistrate in some cases but can be posted without conferring with a magistrate for many minor offenses for which the amount is set in advance.
Also, you can only be constitutionally held for a certain period of time without appearing before a court for an initial appearance at which you are charged and typically you have an attorney assigned for you if you cannot afford one.
Generally speaking a lawyer for a defendant will either post bail on behalf of the client, or will seek to invoke the client's 5th Amendment right to silence and 6th Amendment right to counsel (including the right of a lawyer to visit his client in jail) making further detention much less useful, while challenging law enforcement to articulate probable cause for the arrest with the implication that a civil lawsuit and suppression of evidence and loss of credibility with the local judge could follow if they fail to do so.
If the client is not brought before a court by the constitutional deadline (unusual, but not unheard of), the lawyer can bring this to the attention of the court and have the court demand that his client be brought before the court.
Of course, strictly speaking the defense lawyer can't force the police to do anything. Instead, the defense lawyer persuades the police to do something based upon what a court is likely to do, or has already done, as a result of their conduct so far.
Also, of course, it isn't always possible for a lawyer to get his client out of jail. If the police do have probable cause and the offense is not one for which bail is set in advance, it is not possible for the client to be released until bail is set by a judicial officer such as a magistrate and bail (if granted at all) is posted, which may be beyond the client's means in the case of a serious offense, particularly if the client is considered by the magistrate to be a flight risk.
On TV and books, the person that the police have arrested is usually someone that the police had no probable cause to arrest but suspect of a crime anyway, and the police usually fold when called on the fact that they lack probable cause by the lawyer. Less commonly, on TV and in books, the lawyer facilitates the payment of bail on behalf of his client.