Everyone has touched upon it, but the more succinct version is as follows:
Originally it was not believed that the federal government had any power (absent limited exceptions) to regulate internal actions in the states. Therefore, whether drugs were legal, illegal, some legal some illegal, or any combination was solely a matter for a state government. There was never a constitutional right to use drugs, but in most states you could because a state had declined to regulate them.
During the Progressive Era, many wanted the federal government to take a more active role in the daily lives of citizens, but the damn Constitution got in the way because it had no power. But, hey, some genius said that the feds DO have the power to tax, so we will levy a tax on these drugs and if you don’t pay a tax, bam, federal offense!
The case cited above said that was legitimate even though the purpose was not really to raise revenue, as is the purpose of most taxes, but to regulate.
Starting in the 1930s, the Supreme Court allowed the feds to regulate things directly under the Commerce Clause. Prior to that, the universal consensus would have been that smoking opium in your living room is in no way interstate commerce. You aren’t buying anything and you are smoking it entirely in one state.
After the judicial expansion of the commerce clause, the idea was that even if you grow and smoke opium on your own property, your actions affect the interstate opium market because your actions, when taken in concert with others can affect that interstate market. It was also held that a regulation of an interstate market could amount to a desire to make there be no interstate market at all for a particular commodity.
As a result of these decisions the federal government was able to prohibit drug use, possession and delivery directly (like we have today) without the need to get creative by calling it a tax.