Lots of factors come into play when determining whether a police search was legal. The Fourth Amendment prohibits unreasonable police searches, but what the courts consider "reasonable" depends on the circumstances. For instance, for a home search to be reasonable, police generally need to first get a warrant. The rules for vehicle searches, however, are more relaxed: Several circumstances allow police to search a car with no warrant.
(This article is about federal constitutional law as interpreted by the U.S. Supreme Court. Be mindful that your state law might provide greater protections.)
The general rule is that police searches require a warrant. But there are lots of exceptions to this rule. The "automobile exception" allows police to search a car—without a warrant—so long as they have probable cause to believe there's incriminating evidence inside.
The justifications for the automobile exception are based on two characteristics of cars that make them different from a house. First, the mobility of cars makes it difficult for police to get a warrant before the driver takes off. And second, according to the U.S. Supreme Court, a person has less expectation of privacy in a car than in a home.
A search "incident to arrest" is another exception to the warrant rule that can justify a car search. Basically, when police arrest someone, they can do a limited search—without a warrant—to ensure officer safety or obtain evidence related to the arrest.
The safety justification, however, is valid only if the arrestee is within reaching distance of the car when police conduct the search. For instance, officer safety can't justify a vehicle search when the defendant is already handcuffed in the back of a police car—there's no way a suspect who's secured in that way can break free to get a weapon from his or her own vehicle. (Arizona v. Gant, 556 U.S. 332 (2009).)
Even where no other justification exists, police can search a car if the owner gives permission. The trick with consent is that it must have been "freely and voluntarily given" to be legit; consent that is coerced is invalid, and so is the search that follows. To determine whether consent was voluntary, courts look to all the circumstances of the encounter. (Bumper v. North Carolina, 391 U.S. 543 (1968).)
When police lawfully impound a vehicle, they're allowed to open it and inventory the loose contents. It doesn't matter if police don't have probable cause, because an inventory search—at least in theory—isn't designed to gather evidence. Instead, the theory of inventory searches is that when police tow and store a car, they should be able to make a record of what's inside. An inventory search allows them to protect the car's contents. It also allows them to safeguard themselves—against any dangerous items in the car and claims that they stole or mishandled items.
If police run across incriminating evidence during an inventory search, the evidence can typically be used against the defendant.
Whether a car search is legal—and whether evidence that is seized can be used against you—depends heavily on the facts of your case. A qualified criminal defense attorney will be familiar with the relevant law in your jurisdiction and able to explain your options.