You don't need to have an existing relationship with a lawyer to refuse to talk to the police. You can tell the police you want a lawyer before answering questions. Generally speaking, this should result in the police leaving you alone, giving you time to reach out to an attorney on your own timeline.
This is of course a bit more complicated if you've already been arrested, but in most cases, you'll still be able to make calls out of jail to try to find a lawyer. If you have serious concerns about this kind of situation, having an attorney on retainer would be a good idea.
The business end of the transaction is fairly simple. You would likely sign an engagement agreement with the lawyer in which you agree to pay a modest sum -- $500 or $1,000, imagine, and the lawyer would agree to take your calls when they come in and swoop in to deal with the police as necessary. The lawyer would be required to place your money in a trust account and not touch it until you call him to use his services.
If you're expecting the lawyer to go further by actually appearing in court for you, filing motions, defending you at trial, etc., the retainer would likely be substantially higher.