They likely based it on a reasonable reading of the commerce clause. The commerce clause is subject to being abused, and in my opinion often has been abused in the past to extend federal power over matters clearly beyond any reasonable interpretation of its reach.
It is also my opinion, and apparently an opinion shared by a majority of the court, that it is not an overbroad interpretation of the commerce clause to read it as giving Congress the power to regulate or prohibit the sale or use of marijuana.
Now please note this well: This decision does NOT mean the pro-pot voices have lost the war. It merely means they will have to continue the fight in CONGRESS, and not in the federal courts.
Alternatively, they could try to amend the commerce clause to disempower Congress from legislating in such cases.
The pot heads still have all sorts of options. You should not look to the federal courts for salvation to practice their favorite vice. Debate and persuade, elect and remove. If the facts really are on the pot heads' side, they should win in a walk whether via legislation or by amendment.
That is always the best way in a Republic. That is how it is designed to operate.
Pot heads? Do you call advocates of religious freedom for for Mormons "Mormon heads?"