The U.S. Supreme Court on Thursday rejected a challenge to the reach of the Obama health care law, rescuing the program from a potentially fatal legal challenge for the second time since Obamacare's inception.

By a 6-3 vote, the justices said consumers qualify for a subsidy that lowers the cost of premiums whether they buy their coverage through federal or state exchanges. Chief Justice John Roberts wrote the opinion.

“Congress passed the Affordable Care Act to improve health insurance markets, not to destroy them. If at all possible, we must interpret the Act in a way that is consistent with the former, and avoids the latter,” the court wrote in its majority opinion.

Justice Antonin Scalia, writing for the dissenters, said people should start calling the law "SCOTUScare."

“This Court, however, concludes that this limitation would prevent the rest of the Act from working as well as hoped. So it rewrites the law to make tax credits available everywhere. We should start calling this law SCOTUScare.”

More than six million lower-income Americans who get their health insurance through the federal marketplace or exchange — HealthCare.Gov — depend on the subsidies, reducing their premiums an average of 72 percent, saving an average of $270 a month.

Opponents of the law claimed that the actual wording of the Affordable Care Act passed by Congress made subsidies available only to insurance customers who bought their policies through "an exchange established by the state" where the policyholders live.

Let our news meet your inbox. The news and stories that matters, delivered weekday mornings. This site is protected by recaptcha

NBC

If the challengers had prevailed, customers who bought their insurance on the federal exchange — by far the majority of those insured by Obamacare — would have lost the subsidies. Only 16 states now have their own health exchanges up and running.

The health insurance industry had warned that if the challenge succeeded, the Affordable Care Act would have entered a "death spiral" — with costs rising for a shrinking number of participants, eventually causing the system to collapse.

Among the law's provisions are requirements that insurance companies cover people with pre-existing conditions and that nearly all Americans obtain health insurance. Congress knew that those components of the health care system would not work, the Obama administration had argued, if the subsidies that make insurance affordable for millions of people were available only on state exchanges.

Related: Americans Support Administration on Obamacare - Poll

Most Americans wanted the Supreme Court to side with the government on deciding whether the feds can continue subsidizing insurance premiums in all 50 states under the health care law, according to polls in recent months. Few, however, had much confidence that the court would rule objectively in the case, King v. Burwell.

Outside of the court the mood was jubilant.

Dozens of people outside of the Supreme Court building steps held up signs with the abbreviations of states and the number of people now covered there. The Center for American Progress organized the gathering and people chanted "ACA is here to stay" and "Fight, fight, fight. Healthcare is a human right."

"I agree that healthcare is a human right and I'm here today because I feel that the Affordable Care Act takes a big step forward in securing that right for millions of Americans across the country," said Krystin Racine of Vermont. "I think the most important thing is visibility. I think it's important that people see that there are people who support the law and that it has helped a lot of people."

Meanwhile, doctors groups and patients breathed a sigh of relief Thursday when the Supreme Court upheld one of the most important provisions of Obamacare: the federal subsidies.

"We did dodge a bullet. In the short run if this had gone the other way, then millions of people would have lost health insurance," said Tal Gross, a health policy expert at Columbia University's school of public health.

Related: Will Republicans Finally Accept Obamacare?

Conservatives vowed to press on with their efforts to bring attention to flaws in a law they see as broken.

"That we're even discussing another of Obamacare's self-inflicted brushes with the brink - again - is the latest indictment of a law that's been a rolling disaster for the American people," Senate Majority Leader Mitch McConnell said on the Senate floor Thursday after the court ruling was announced. "Today's ruling won't change Obamacare's multitude of broken promises, including the one that resulted in millions of Americans losing the coverage they had and wanted to keep. Today's ruling won't change Obamacare's spectacular flops, from humiliating website debacles to the total collapse of exchanges in states run by the law's loudest cheerleaders. Today's ruling won't change the skyrocketing costs in premiums, deductibles, and co-pays that have hit the middle class so hard over the last few years."

The White House, however, remained undaunted.

"Today is a victory for hardworking Americans all across this country whose lives will continue to become more secure in a changing economy because of this law," the president said on Thursday in remarks after the ruling.

— Sam Gringlas contributed