As a general rule, here is what to do if you are confronted by the police in the US:
1. Try to ascertain why they want to talk to you before answering anything. Remember that they are allowed to lie to you to get you to say things and that they are probably not just being your friends when they strike up a conversation. When they have their uniforms on, they are working. When they are working, they are generally not fucking around, unless they are at the donut shop. Therefore, they are probably talking to you for a reason. Unless you are 100% sure that they are not after you, you should probably avoid talking to them if at all possible.
2. If you cannot conclusively ascertain that they are not after you, then you should politely decline to talk to them and then excuse yourself (don't ask for permission to leave). A scripted answer might be: "Sorry, I can't talk now. Excuse me, I have to be going." You then leave.
Under no circumstances should you try to outwit the police or talk your way out of situations. 99% of the time, you will simply dig yourself a deeper hole and you will regret it later (when talking to your court-appointed attorney 3 minutes before your preliminary hearing). More criminal cases are lost because the defendant (i.e. you) won't shut his fucking face than for any other reason.
3. If they tell you that you cannot excuse yourself, ask to leave. Say something like, "I can't talk now, I'm going to leave. Can I go?" Or if you simply try to leave and they tell you that you cannot, ask them "Are you asking me or are you telling me?" These are not magic words, but they help you to ascertain whether your interaction with the police is mere chit-chat from the local jack-booted fascist thug, or whether they are actually interested in you much as the bull prisoner is interested in the fresh meat at the hoosegow. Also, if they tell you that you cannot leave, there are significant legal implications. You are in custody, though you may not be under arrest at that point.
4. If the police will not let you go (they are "telling you") then you should absolutely and immediately shut up. Say nothing _at all_ from that point forward. Giving your name is ok. So is saying "I want to talk to a lawyer". Anything else could create problems for you, even an answer (verbal or not) to something as simple as "Is this your car?"
5. If they ask for permission to search something, do not give it. If they want to look through your wallet, do not give it to them. Make them take it from your pocket. If they want to open your car, make them get the keys out of your pocket -- do not give them the keys.
They will never ask you "Can I search your car for drugs?" They will ask "you don't mind if I look in your car, do you?" They will do it casually and conversationally. They have obtained consent from hundreds of drivers before and they know that you are likely scared out of your mind or at least somewhat intimidated (especially after they shine lights in your face, bark at you demanding ID and papers, etc.). They know how to make you do the things that they want you to do -- i.e. waive your rights so that they can root through your things without a warrant or try to extract a confession from you.
Do not play lawyer with them. Don't tell them that they need to get a warrant. They know that. They know search and seizure law much better than you think. They can manufacture exigent circumstances more easily than you can imagine. "Plain view"? They know all about it. If they take your keys out of your pocket and open your trunk, there's not much you can do about it anyway. Arguing with them will just make them more annoyed. It is far, far better to sit there quietly and to let them go about their business.
Also, please remember that they have searched hundreds of cars. There is not a hiding place that they do not know about. They will find your bag of dope or your vial of crack if you let them search the car.
They have the upper hand in experience, training, psychology, etc. You are totally unprepared, and your best defense is the intellectual equivalent of "going limp" at a civil disobedience rally like abortion protestors or anti-globalism people have done for years. Just shut your brain down and play dead. Give them your name. Say "I want to talk to a lawyer." That is it. I cannot emphasize this "do nothing" strategy enough.
6. You will get some version of the great lie at some point: "Just tell me what is going on and I'll go easy on you." Horseshit and double horseshit. The cop is trying to extract a confession and he is lying to you to do it. Cops don't make deals. The D.A. does. Keep your dumbass mouth shut.
7. There are exceptions to every rule. Sometimes it might pay to try to talk your way out of something. Unfortunately, you are stupid and scared and you frequently don't know why the cops are after you. It's even worse if you do know why they are after you.
Your body is probably so jacked up on adrenaline at this point that your brain is completely shut down except for good ol' "fight or flight" and breathing and circulation. Your digestion has actually probably stopped. You are totally unequipped to try to talk yourself out of things and you are totally unequipped to know when such a strategy might pay off. Do the safe thing instead. Shut the fuck up and ask for a lawyer.
Here is a brief summary:
1. If cops want to talk to you, refuse politely and excuse yourself.
2. If the refuse to let you leave, stop talking.
3. If they ask you to do something, ask them "are you asking me or telling me?" If they are "telling" you, then politely do any physical activity that they request (i.e. "get in the police car" or "hold out your arms"). Do not do things such as "show me where you keep the dope" or "open your trunk for me" or "give me your keys".
4. If they refuse to let you leave and they are questioning you, give them your name and ask to speak to a lawyer. Then shut up and say nothing further. If they continue to question you, simply say "I want to talk to a lawyer" and stay quiet.
Finally, some people will tell folks not to be scared when they deal with the police. That is bullshit. Unless you called them, they are probably talking to you because they want to put you in jail. You should be scared shitless. You should be so scared that you'll actually have the balls to (1) stay quiet and (2) politely refuse to cooperate. It's hard. Cops are trained at getting people to talk and getting them to consent. It's classic social engineering and psychology. Don't be stupid. Be quiet.
At this point, I'm just rambling and whining, but it makes my job incredibly difficult when I meet with someone and they have already confessed to owning the drugs that the cops found in their car and that they admitted that they let the cops search the car. It's over at that point, people. Johnny fucking Cochrane can't help you after you confess.