"After more than 50 votes in Congress to repeal or weaken this law, after a presidential election based in part on preserving or repealing this law, after multiple challenges to this law before the Supreme Court, the Affordable Care Act is here to stay," Obama said in the White House Rose Garden.
Chief Justice John Roberts, a conservative appointed by Republican President George W. Bush, wrote in the 6-3 ruling that Congress clearly intended for the tax subsidies that help low- and moderate-income people buy private health insurance to be available in all 50 states.
The court decided that the law, widely known as Obamacare, did not restrict the subsidies to states that establish their own online health insurance exchanges.
"Congress passed the Affordable Care Act to improve health insurance markets, not to destroy them," Roberts wrote in the court's decision, adding that nationwide availability of the credits is required to "avoid the type of calamitous result that Congress plainly meant to avoid."
Roberts was joined by fellow conservative Justice Anthony Kennedy and the court's four liberal members in the majority.
It marked the second time in three years that the high court ruled against a major challenge to the law brought by conservatives. Both rulings were written by Roberts. Republicans have fought the law since its inception.
With the ruling, the current system will remain in place, with subsidies that help low- and moderate-income people buy private health insurance available in all 50 states. If the challengers had won, at least 6.4 million people in at least 34 states would have lost the subsidies whose average value is $272 per month.
"There can be no doubt that this law is working," Obama said. "It has changed, and in some cases saved, American lives. It set this country on a smarter, stronger course."
The decision means the subsidies will remain not just in the 13 states that have set up their own exchanges and the three states that have state-federal hybrid exchanges, but also in the 34 states that use the exchange run by the federal government.
The case centered on the tax subsidies offered under the law, passed by Obama's fellow Democrats in Congress in 2010 over unified Republican opposition. The exchanges are online marketplaces that allow consumers to shop among competing insurance plans.
The question before the justices was whether a four-word phrase in the expansive law saying subsidies are available to those buying insurance on exchanges "established by the state" has been correctly interpreted by the administration to allow subsidies to be available nationwide
Roberts wrote that although the conservative challengers' arguments about the plain meaning of the statute were "strong," the "context and structure of the act compel us to depart from what would otherwise be the most natural reading of the pertinent statutory phrase."
Scalia dissents
After Chief Justice Roberts announced the decision from the bench, Justice Antonin Scalia read for 11 minutes from his dissenting opinion.
Scalia said the statute's words were clear, that Congress wanted to limit the credits to the state exchanges. Scalia recalled the court's 2012 decision narrowly upholding the law, again over his dissent.
"We really should start calling the law SCOTUScare," Scalia said. SCOTUS is the acronym for the Supreme Court of the United States.
"This court has no free-floating power to rescue Congress from its drafting mistakes," Scalia added.
Roberts, sitting next to him on the bench, sat stone-faced. He smiled slightly at the SCOTUScare line, but otherwise betrayed no emotion.
Conservative Justices Clarence Thomas and Samuel Alito joined Scalia's dissent.
The court, in another ruling favoring the Obama administration on Thursday, also embraced a broad interpretation of the type of discrimination claims that can be made under the landmark Fair Housing Act.
The Obamacare ruling will come as a major relief to Obama as he seeks to ensure that his legacy legislative achievement is implemented effectively and survives political and legal attacks before he leaves office in early 2017.
"The subsidies upheld today help patients afford health insurance so they can see a doctor when they need one and not have to wait until a small health problem becomes a crisis," said Dr. Steven Stack, president of the American Medical Association.
Shares of hospital operators, health services providers and insurers rallied broadly following the court's decision to uphold the subsidies. Top gainers included hospital companies Tenet Healthcare Corp., up 8.8 percent, and Community Health Systems Inc., up 8.5 percent.
The Democratic-backed law aimed to help millions of Americans who lacked any health insurance afford coverage.
The Obama administration has hailed the law as a success, saying 16.4 million previously uninsured people have gained health insurance since it was enacted. There are currently around 26 million people without health insurance, according to government figures.
Conservatives have called the law a government overreach and "socialized medicine." Opponents repeatedly but unsuccessfully sought to repeal it in Congress and launched a series of legal challenges. In 2012, Roberts cast the deciding vote in a 5-4 decision that upheld the law on constitutional grounds, siding with the court's four liberals.
The current case started as a long-shot legal challenge by conservative lawyers that oppose the law. Financed by a libertarian Washington group called the Competitive Enterprise Institute, the lawyers recruited four people from Virginia to be the plaintiffs. The lead plaintiff was a self-employed limousine driver named David King.
The plaintiffs said they were "deeply disappointed" with the ruling. The law "unfairly restricts the health insurance choices of millions of people, and it threatens their jobs as well," they added.
The challengers said that the four-word phrase in the law indicates that only people who have bought insurance on state-established exchanges qualify for the tax-credit subsidies.
The Obama administration, backed by the healthcare industry, said other provisions in the law made clear that Congress intended the subsidies to be available nationwide regardless of whether states set up their own exchanges or leave the task to the federal government.