First off, I am a lawyer (a criminal defense attorney). Here's how a traffic stop legally works, according to the U.S. Supreme Court. First off, a warrant is a presumed requirement for any search or seizure. A traffic stop is legally considered to be a seizure (of your person and your car). However, there are many exceptions to the warrant requirement. One exception is for an investigative stop when the cop has reasonable suspicion of a crime being committed. Reasonable suspicion means "sufficient knowledge to believe that criminal activity is at hand," and it must be supported by specific and articulable facts (just a hunch is not enough).
For a traffic stop, reasonable suspicion is usually met when the cop clocks you over the limit, or actually witnesses something illegal about your vehicle or your driving. Once reasonable suspicion is established, the cop has the authority to stop you for the limited purpose of investigating the crime he has reasonable suspicion of. The investigation must be limited in scope and duration to what is necessary for investigating the crime for which there is reasonable suspicion, and for performing any administrative actions necessary (such as writing a ticket). The scope of such a stop does not extend to any additional searches or seizures beyond the initial stop.
During the stop, the officer can talk to you and ask you questions, even if they have nothing to do with the stop (as long as the questioning isn't so prolonged that it causes the duration of the stop to exceed the limits mentioned above). Also, a cop can ask you to consent to a search or seizure, even if he doesn't have any reason to think you have any contraband. Consent is another exception to the warrant requirement, and you generally can't get any evidence excluded in court if it is found pursuant to a search that you consented to. It is possible that in talking to you and observing you and your vehicle, reasonable suspicion of additional crimes might arise, which could extend the allowable duration of the stop. Also, under the "plain view doctrine," if it is immediately apparent that something in plain view in your vehicle is contraband, the officer may seize it as evidence. This is another exception to the warrant requirement.
If at any time during a stop the officer comes to have reasonable suspicion that a suspect is both armed and dangerous, he may engage in a "pat-down" of the person's outer clothing, purely for the purpose of officer safety, to see if the person is carrying any dangerous weapons. If any are found, they may be seized for the duration of the stop. Also, under the "plain feel doctrine," if during a pat-down it is immediately apparent that something he feels is contraband, he can pull it out and investigate, even if it is clearly not a weapon or anything dangerous to the officer.
Another exception to the warrant requirement, which could allow a cop to make a more in-depth search of your property, is the motor vehicle exigency. If a cop has probable cause that a crime is being committed, and that evidence of said crime is in your vehicle, he may search the vehicle anywhere such evidence could reasonably be found. Probable cause means "a reasonable belief that a person has committed a crime." It is a higher standard than reasonable suspicion. Like reasonable suspicion, it must be supported by specific and articulable facts, and must relate to a specific crime (a cop cannot say that he has probable cause of "general criminal activity" or something like that).
Another exception to the warrant requirement is a search incident to arrest. If a cop has probable cause that someone committed a crime, he may arrest them for the purpose of taking them to a place of confinement, to allow criminal legal proceedings to begin (arrest is another exception to the warrant requirement). When someone is arrested, the cop may search the arrestee for dangerous weapons. The same rules apply as for a "pat down." Also, the cop can search the immediate area around where the arrestee is for the same purpose. If the arrestee is not presently securely confined (such as in the back of a cop car), the cop may also search the interior compartment of any vehicle the person recently occupied for the same purpose.
Another exception to the warrant requirement is a custodial search. If the cop takes a person or property into custody, he may search and inventory all of said person's belongings or all of said property's contents, as long as there is an official policy to do so in effect. The purpose is administrative -- to ensure that all property is accounted for. This happens most often when someone is arrested, and when someone's car is towed.
So though a traffic stop is initially only for reasonable suspicion/probable cause of violation of a traffic ordinance, there are a variety of ways that it can escalate to a more invasive search or seizure if certain standards are met.
Especially while being disarmed, your best bet is to comply with the demands of officers (even if illegal), while making it clear that you do not consent to the search or seizure. Any violations of the law can be hashed out after the fact, either up the chain of command in the agency, or in court.
Asking if you are free to leave is pretty much a way of saying "either write me a ticket, or let me be on my way." Really the only reason to say this would be if you think a cop has exceeded the legal scope of his stop, or if he is asking questions that you don't want to answer. If the cop hasn't finished his stop, he will just tell you "no." If he has already exceeded the permitted duration of his stop or has already given the ticket or warning, legally he should tell you that yes, you are free to leave and let that be the end of it.