Whether a police officer can detain you on a traffic stop depends on their ability to meet constitutional standards. If they don't meet these standards, they may be liable for your wrongful detention and/or arrest.
The answer to that question is pretty simple: the police can only detain you for a reasonable period while they investigate a criminal offense. This includes asking you questions about the crime, searching your car, and making an arrest.
If you're pulled over, stay calm and don't resist or yell at the officers. In most cases, resisting an officer's authority is a criminal offense.
Often, police will try to ask you questions about your drugs or drug use. If they do this, you should politely tell them that you will not answer questions without legal representation.
Once the investigation is over, the police must let you go. Otherwise, they're violating your Fourth Amendment right to remain silent.