Barack Obama's re-election hopes received a major boost on Thursday when the US supreme court upheld his landmark healthcare reforms in a historic ruling the president hailed as a victory for the entire country.

In a decision which ensures that the political battle over healthcare continues to election day in November, the divided court upheld the Patient Protection and Affordable Care Act on the grounds that its controversial central provision – the requirement for almost all Americans to buy health insurance, known as the individual mandate – is legal because the measure amounts to a tax.

The chief justice, John Roberts, provided the crucial vote in joining the four more liberal judges in upholding the law. The four conservative judges held that the law is "invalid in its entirety".

Obama, who was given no preview of the decision and had to wait for the announcement just like everyone else, was reported to have been euphoric when the news came through.

Speaking from the White House, he said: "Whatever the politics, today's decision was a victory for people all over this country whose lives will be more secure because of this law and the supreme court's decision to uphold it."

The press conference was one of the rare occasions since 2010 that Obama has trumpeted his healthcare reforms, mainly because it has been seen by his election strategists as a vote-loser.

Obama spoke in glowing terms about what he claimed were the benefits of the reform. "The highest court in the land has now spoken. We will continue to implement this law. And we'll work together to improve on it where we can. But what we won't do – what the country can't afford to do – is refight the political battles of two years ago, or go back to the way things were."

He predicted that Americans will eventually come around. "Today, I'm as confident as ever that when we look back five years from now, or ten years from now, or 20 years from now, we'll be better off because we had the courage to pass this law and keep moving forward."

But Mitt Romney, the Republican presidential challenger, pledged to repeal the healthcare legislation if he wins the election. In spite of the supreme court ruling, Romney said the court had only ruled on its constitutionality, not whether it is a good law or good policy.

"Obamacare was bad policy yesterday, it's bad policy today. Obamacare was bad law yesterday, it's bad law today.

Romney continued: "Obamacare is a job killer. Businesses across the country have been asked what the impact is of Obamacare … it makes them less likely to hire people.

"This is a time of choice for the American people. Our mission is clear. If we want to get rid of Obamacare, we're going to have to replace President Obama. My mission is to make sure we do exactly that."

Though conservatives voiced fury over the ruling, the Romney campaign believes it could work in their favour in the long term as it will galvanise voters opposed to the law to vote against Obama in November.

Polls consistenly show that a majority of Americans are opposed to the central plank of the law, the individual mandate, which from 2014 will require them to purchase health insurance or face penalties through their taxes. According to members of the Romney campaign team, they raised more than $1m in new donations in the three hours after the court ruled.

The Republican House speaker, John Boehner, insisted the act was still a bad law. "Today's ruling underscores the urgency of repealing this harmful law in its entirety." The House is scheduled to vote to repeal the legislation on July 11, but it is purely symbolic.

The Republican leader in the senate, Mitch McConnell, echoed Boehner. "Today's decision does nothing to diminish the fact that Obamacare's mandates, tax hikes, and Medicare cuts should be repealed and replaced with common sense reforms that lower costs and that the American people actually want. It is my hope that with new leadership in the White House and senate, we can enact these step-by-step solutions and prevent further damage from this terrible law," McConnell said.

But even if Romney does win, it will be hard to repeal the law without a Republican majority of 60 in the 100-member senate, an outcome the party is unlikely to achieve in November.

Democrats in Congress joined the White House in celebrating the victory. The Democratic leader in the senate, Harry Reid, said: "The supreme court has spoken, the matter is settled."

The case was the most politically loaded to come before the court since the decision on the outcome of the 2000 presidential election between George Bush and Al Gore.

John Roberts, the supreme court's chief justice, who wrote the majority decision, said that the 26 US states that challenged the legality of the health reforms had a case under a clause in the constitution governing interstate commerce.

But, in a surprise move, he ruled that while the individual mandate is not itself a tax, the penalties imposed for not buying health insurance are and that therefore the entire requirement falls within the remit of Congress's right to impose taxes.

"The Affordable Care Act's requirement that certain individuals pay a financial penalty for not obtaining health insurance may reasonably be characterised as a tax. Because the Constitution permits such a tax, it is not our role to forbid it, or to pass upon its wisdom or fairness," the judgment read.

Roberts also said it was not for the court to question the decisions of Congress. "It is not our job to protect the people from the consequences of their political choices," he wrote.

But the ruling did not go entirely Obama's way, with the court challenging one part of the law designed to force individual states to comply with the programme by threatening to withdraw their Medicaid funding. The court ruled that while the federal government could withhold new Medicaid funding, it would be unconstitutional to remove existing money.