WASHINGTON — The Supreme Court on Thursday upheld President Obama’s health care overhaul law, saying its requirement that most Americans obtain insurance or pay a penalty was authorized by Congress’s power to levy taxes. The vote was 5 to 4, with Chief Justice John G. Roberts Jr. joining the court’s four more liberal members.

The decision was a victory for Mr. Obama and Congressional Democrats, affirming the central legislative achievement of Mr. Obama’s presidency.

“The Affordable Care Act’s requirement that certain individuals pay a financial penalty for not obtaining health insurance may reasonably be characterized as a tax,” Chief Justice Roberts wrote in the majority opinion. “Because the Constitution permits such a tax, it is not our role to forbid it, or to pass upon its wisdom or fairness.”

At the same time, the court rejected the argument that the administration had pressed most vigorously in support of the law, that its individual mandate was justified by Congress’s power to regulate interstate commerce. The vote was again 5 to 4, but in this instance Chief Justice Roberts and the court’s four more conservative members were in agreement.

The court also substantially limited the law’s expansion of Medicaid, the joint federal-state program that provides health care to poor and disabled people. Seven justices agreed that Congress had exceeded its constitutional authority by coercing states into participating in the expansion by threatening them with the loss of existing federal payments.

Justice Anthony M. Kennedy, who had been thought to be the administration’s best hope to provide a fifth vote to uphold the law, joined three more conservative members in an unusual jointly written dissent that said the court should have struck down the entire law. The majority’s approach, he said from the bench, “amounts to a vast judicial overreaching.”

The court’s ruling was the most significant federalism decision since the New Deal and the most closely watched case since Bush v. Gore in 2000. It was a crucial milestone for the law, the Patient Protection and Affordable Care Act of 2010, allowing almost all — and perhaps, in the end, all — of its far-reaching changes to roll forward.

Mr. Obama welcomed the court’s decision on the health care law, which has inspired fierce protests, legal challenges and vows of repeal since it was passed. “Whatever the politics, today’s decision was a victory for people all over this country whose lives are more secure because of this law,” he said at the White House.

Republicans, though, used the occasion to attack it again.

“Obamacare was bad policy yesterday; it’s bad policy today,” Mitt Romney, the presumptive Republican presidential nominee, said in remarks near the Capitol. “Obamacare was bad law yesterday; it’s bad law today.” He, like Congressional Republicans, renewed his pledge to undo the law.

The historic decision, coming after three days of lively oral arguments in March and in the midst of a presidential campaign, drew intense attention across the nation. Outside the court, more than 1,000 people gathered — packing the sidewalk, playing music, chanting slogans — and a loud cheer went up as word spread that the law had been largely upheld. Chants of “Yes we can!” rang out, but the ruling also provoked disappointment among Tea Party supporters.

In Loudoun County, Va., Angela Laws, 58, the owner of a cleaning service, said she and her fiancé were relieved at the news. “We laughed, and we shouted with joy and hugged each other,” she said, explaining that she had been unable to get insurance because of her diabetes and back problems until a provision in the health care law went into effect.

After months of uncertainty about the law’s fate, the court’s ruling provides some clarity — and perhaps an alert — to states, insurers, employers and consumers about what they are required to do by 2014, when much of the law comes into force.

The Obama administration had argued that the mandate was necessary because it allowed other provisions of the law to function: those overhauling the way insurance is sold and those preventing sick people from being denied or charged extra for insurance. The mandate’s supporters had said it was necessary to ensure that not only sick people but also healthy individuals would sign up for coverage, keeping insurance premiums more affordable.

Conservatives took comfort from two parts of the decision: the new limits it placed on federal regulation of commerce and on the conditions the federal government may impose on money it gives the states.

Five justices accepted the argument that had been at the heart of the challenges brought by 26 states and other plaintiffs: that the federal government is not permitted to force individuals not engaged in commercial activities to buy services they do not want. That was a stunning victory for a theory pressed by a small band of conservative and libertarian lawyers. Most members of the legal academy view the theory as misguided,if not frivolous.

“To an economist, perhaps, there is no difference between activity and inactivity; both have measurable economic effects on commerce,” Chief Justice Roberts wrote. “But the distinction between doing something and doing nothing would not have been lost on the framers, who were practical statesmen, not metaphysical philosophers.”

Justice Ruth Bader Ginsburg, in an opinion joined by Justices Stephen G. Breyer, Sonia Sotomayor and Elena Kagan, dissented on this point, calling the view “stunningly retrogressive.” She wondered why Chief Justice Roberts had seen fit to address it at all in light of his vote to uphold the mandate under the tax power.

Akhil Reed Amar, a Yale law professor and a champion of the health care law, said that it was “important to look at the dark cloud behind the silver lining.”

“Federal power has more restrictions on it,” he said, referring to the new limits on regulating commerce. “Going forward, there may even be laws on the books that have to be re-examined.”

The restrictions placed on the Medicaid expansion may also have significant ripple effects. A splintered group of justices effectively revised the law to allow states to choose between participating in the expansion while receiving additional payments or forgoing the expansion and retaining the existing payments. The law had called for an all-or-nothing choice.

The expansion had been designed to provide coverage to 17 million Americans. While some states have indicated that they will participate in the expansion, others may be resistant, leaving more people outside the safety net than the Obama administration had intended.

Although the decision did not turn on it, the back-and-forth between Justice Ginsburg’s opinion for the four liberals and the joint opinion by the four conservatives — Justice Kennedy and Justices Antonin Scalia, Clarence Thomas and Samuel A. Alito Jr. — revisited the by-now-familiar arguments. Broccoli made a dozen appearances.

“Although an individual might buy a car or a crown of broccoli one day, there is no certainty she will ever do so,” Justice Ginsburg wrote. “And if she eventually wants a car or has a craving for broccoli, she will be obliged to pay at the counter before receiving the vehicle or nourishment. She will get no free ride or food, at the expense of another consumer forced to pay an inflated price.”

The conservative dissenters responded that “one day the failure of some of the public to purchase American cars may endanger the existence of domestic automobile manufacturers; or the failure of some to eat broccoli may be found to deprive them of a newly discovered cancer-fighting chemical which only that food contains, producing health care costs that are a burden on the rest of us.”

All of the justices agreed that their review of the health care law was not barred by the Anti-Injunction Act, which allows suits over some sorts of taxes only after they become due. That could have delayed the health care challenge to 2015. The conservative dissenters said that the majority could not have it both ways by calling the mandate a tax for some purposes but not others.

“That carries verbal wizardry too far, deep into the forbidden land of sophists,” they said.

As a general matter, Chief Justice Roberts wrote that the decision in the case, National Federation of Independent Business v. Sebelius, No. 11-393, offered no endorsement of the law’s wisdom.

Some decisions, the chief justice said, “are entrusted to our nation’s elected leaders, who can be thrown out of office if the people disagree with them.”

Justice Ginsburg, speaking to a crowded courtroom that sat rapt for the better part of an hour, drew a different conclusion.

Reporting was contributed by John H. Cushman Jr., Robert Pear, John Schwartz, Ethan Bronner and Sabrina Tavernise.

A version of this article appears in print on June 29, 2012, on page A1 of the New York edition with the headline: Justices, By 5-4, Uphold Health Care Law; Roberts In Majority; Victory For Obama. Order Reprints|Today's Paper|Subscribe