A police officer comes to the door, knocks, and asks to come in. You turn him down, asking for a warrant, which he doesn't have. The officer forces his way in anyway, finds illegal drugs hidden in a cabinet in the kitchen, and arrests you.
Is it legal? The officer believed you had illegal drugs, and he was correct. Technically, you were breaking the law by possessing them.
Even so, this type of search is not legal. The U.S. Constitution, through the Fourth Amendment, says that citizens have a right to due process. Searches and seizures have to be done lawfully, which generally means getting a warrant first. Officers can't just force their way into homes at random, on a whim, and look around until they discover something that breaks the law.
There are rare cases in which warrants aren't needed. For instance, drugs that are considered to be in "plain view" can sometimes be seized without a warrant, as long as the other proper steps were followed. If you forgot the drugs were on the table and invited the officer in without giving him permission to dig around, for example, they may still be fair game.
So, what happens if the rights granted by the Fourth Amendment are violated? Typically, the related evidence is then thrown out of court. Even if police do know that you had illegal drugs, they can't use them against you since they themselves broke the law to obtain them. Police are not above the law.
As you can see, it's critical to know your rights. If they're violated by the authorities, be sure you understand what legal options you have moving forward.
Source: FindLaw, "Drug Possession Defenses," accessed May 11, 2017