Chief Justice John Roberts, who wrote the prevailing 59-page opinion, abandoned his fellow conservatives and said the mandate can't be upheld under Congress' broad power to regulate interstate commerce but can be upheld under its power to tax in the Constitution's spending clause.
The ruling by any standard is an enormous legal and political victory for Obama, who hailed the decision as "good for the country ... good for the American people."
"I didn't do this because it was good politics," Obama said, adding discussions about who wins and who loses under the decision misses the point: "It was a victory for people all over this country whose lives will be more secure."
Obama's likely opponent in November, Republican Mitt Romney, disagreed vowing, if he's elected, to "repeal and replace Obamacare."
"It did not say it is good law or good policy. It was bad policy yesterday and it's bad policy today. It was bad law yesterday and bad law today," Romney said.
"Our precedent demonstrates that Congress had the power to impose the [penalty for failing to buy health insurance] under the taxing power, and that [the relevant section of the Affordable Care Act] need not be read to do more than impose a tax. This is sufficient to sustain it," Roberts wrote.
The individual mandate is set to be implemented in 2014. When it does come into play, anyone who can afford insurance and doesn't purchase it could be fined as much as 2.5 percent of income -- hence the tax.
But in a partial defeat for the administration, Roberts said the law cannot threaten to withhold Medicaid funds from states that do not participate in the ACA's expansion of that program. That part of the ruling was joined by a plurality, not a majority of the justices.
"Nothing in our opinion precludes Congress from offering funds under the ACA to expand the availability of healthcare, and requiring that states accepting such funds comply with the conditions on their use," Roberts wrote. "What Congress is not free to do is to penalize states that choose not to participate in that new program by taking away their existing Medicaid funding."
The chief justice added, "Our permissive reading of these powers is explained in part by a general reticence to invalidate the acts of the nation's elected leaders."
But he also said the ruling does not imply approval: "The framers [of the Constitution] created a federal government of limited powers, and assigned to this court the duty of enforcing those limits. The court does so today. But the court does not express any opinion on the wisdom of the Affordable Care Act. Under the Constitution, that judgment is reserved to the people."
The ruling means that more popular parts of the law -- allowing parents to include children on their health insurance until age 26, removing the caps on healthcare expenditures and removing "the pre-existing" restrictions on health insurance, survive as well.
All four dissenting justices joined in a single opinion and issued separate opinions.
Reading the joint dissent by the four conservative justices, Justice Anthony Kennedy said the entire law should be struck down.
"Congress has set out to remedy the problem that the best healthcare is beyond the reach of many Americans who cannot afford it," the conservatives said. "It can assuredly do that, by exercising the powers accorded to it under the Constitution. The question in this case, however, is whether the complex structures and provisions of the Patient Protection and Affordable Care Act ... go beyond those powers. We conclude that they do."
Roberts opinion did not reach the question of whether the 1867 Anti-Injunction Act, which bars suits against the federal government over a "tax" until the tax actually goes into effect and someone actually is required to pay it, applies to the challenge by 26 states to the individual mandate since the mandate was upheld.
The mandate, the core provision of the law and one that gives health insurers the funds to comply with all of the act's provisions, was challenged by a national business group as well as the 26 states.
The ruling upholds in part and reverses in part a ruling by a U.S. appeals court in California.