Media playback is unsupported on your device Media caption Watch: A history of Obamacare challenges

The US Supreme Court has upheld a key portion of President Barack Obama's healthcare law, preserving health insurance for millions of Americans.

In a 6-3 decision, the justices said that tax subsidies that make health insurance affordable for low-income individuals can continue.

The ruling preserves the law known as Obamacare, which Mr Obama considers a major part of his presidential legacy.

Republicans have vowed to continue fighting the law.

"We've got more work to do, but what we're not going to do is unravel what has now been woven into the fabric of America," Mr Obama said.

The case, known as King v Burwell, was the second major challenge the law has faced in the US's highest court.

Unlike in many other western countries, the US does not have a single-payer healthcare system. Private companies, rather than the US government, provide health insurance for US citizens.

Image caption The decision sparked celebrations outside the court in Washington

The enactment of the Affordable Care Act (ACA) - one of Mr Obama's most significant and controversial domestic achievements - in 2010 mandated that every American had to purchase private insurance. It provided the subsidies to allow many to do so.

In 2012, the mandate portion of the law was challenged in the court. The justices ruled to preserve it.

In that decision, as in the decision on Thursday, Chief Justice John Roberts surprised observers by siding with his liberal colleagues in support of the law.

"Congress passed the Affordable Care act to improve health insurance markets, not to destroy them," Chief Justice John Roberts wrote in the opinion.

Justice Anthony Kennedy dissented in 2012, but sided with the majority on Thursday.

Had the court made the opposite decision, an estimated 8.7 million people in the US would have been at risk of losing the aid that makes healthcare affordable.

Analysis - Jon Sopel, BBC North America editor

The stakes could not have been higher.

People's health (crucially important) and Obama's legacy (less important, but for him and those around him fairly vital) were at stake.

Well a politically finely balanced Supreme Court has given an emphatic, overwhelming vote in favour of the president by 6-3.

I bet "No-drama Obama" is high-fiving anyone and everyone in the White House - that is how big it is.

Obama defies lame-duck expectations

Demonstrators gathered outside the court as early on Thursday morning.

Reading updates on their mobile phones, the crowd became jubilant when they learned mid-morning that the court had ruled in their favour. Some began dancing, while others chanted "If you're covered and you know it clap your hands."

"This is a big sigh of relief for millions across the country," said Ron Pollack of Families USA, a health-care advocacy organisation. "The ACA is not just the law of the land, it will remain the law of the land".

"Today is a good day for healthcare in America," said activist Benton Strong. "I hope this is the end of the line."

Demonstrators opposing the subsidies did not have a large visible presence.

Image caption Demonstrators added tape that read "still covered" to their signs after the decision was announced

Image copyright Getty Images Image caption Media staff sprinted with the news from the court to colleagues outside

Justice Antonin Scalia wrote in his dissent that the Supreme Court is setting a precedent of favouring some laws over others.

"We should start calling this law Scotuscare" Justice Scalia wrote, referring to the court's acronym. "Today's interpretation is not merely unnatural; it is unheard of."

Congressional Republicans have voted more than 50 times to undo the law.

House Speaker John Boehner said that they will continue their "efforts to repeal the law and replace it with patient-centred solutions that meet the needs of seniors, small business owners, and middle-class families".

Following the enactment of the ACA in 2010, states were given the option of establishing their own healthcare exchanges - online marketplaces for citizens to buy health coverage.

Citizens in states that refused to establish exchanges could shop for coverage on a federal exchange.

Media playback is unsupported on your device Media caption President Obama hailed the decision saying the Affordable Healthcare Act was "here to stay"

Image copyright Reuters Image caption Rep Steve Scalise described the ACA as a "dismal failure" and vowed to continue fighting it

In the court, opponents argued that a phrase included in the law, "established by the state," meant the federal government could only provide subsidies to people in states that set up their own exchanges.

However, most Americans receiving subsidies purchase healthcare through the federal exchange, after many states decided not to set up their own marketplaces. Only 13 states and Washington DC have set up their own exchanges.

The Obama administration argued that was a too-narrow reading of the law, which spans nearly 1,000 pages, and the rest of the legislation makes clear subsidies are intended for those who meet income requirements, regardless of which exchange insurance was purchased from.

Obamacare by the numbers

citizens in 37 states depend on federal subsidies to make healthcare affordable

depend on federal subsidies to make healthcare affordable only 13 states and Washington, DC have established their own exchanges

have established their own exchanges over 10m people have purchased coverage through one of the new exchanges - federal or state

have purchased coverage through one of the new exchanges - federal or state on average, the federal government provides a $272 (£173) monthly subsidy

Source: Reuters

The upholding of the law cements President Obama's biggest legislative victory. Limiting the subsidies could have unravelled Mr Obama's signature healthcare reforms.

Republican Congressional leader Steve Scalise said he was disappointed with the ruling and would work to have the law "repealed and replaced," echoing near-universal Republican sentiment.

"It does not change the fact that Obamacare has been a dismal failure for millions of Americans who have lost the good healthcare that they liked, and are paying more for the plans that they have," Mr Scalise said in statement.

Meet the Supremes

the US Supreme Court is one of the oldest constitutional review courts in the world

their decisions can overturn federal or state laws, if the justices find they are unconstitutional

its powers have ended segregation in schools, legalised interracial marriage and once effectively chose a president

there are nine justices nominated by the president and confirmed by the Senate, each with life tenure

it is currently one third female, with one Hispanic justice and one African-American

Meet the Supremes: Who are the US Supreme Court Justices