The third attempt to derail the Affordable Care Act at the Supreme Court suffered the same unsuccessful fate of the first two on Thursday, and jubilant Democrats praised the decision preserving the law that now delivers health-care coverage to millions of Americans.

On a 7-to-2 vote, the court dismissed the latest challenge from Republican-led states and endorsed by the Trump administration. Justice Stephen G. Breyer’s somewhat technical opinion said neither the states nor individual plaintiffs had legal standing to challenge the law, which also survived challenges in 2012 and 2015.

“This ruling reaffirms what we have long known to be true: the Affordable Care Act is here to stay,” tweeted former president Barack Obama. The act is the landmark domestic achievement of his presidency, and is now known by both supporters and detractors as Obamacare.

President Biden said he plans to build on the program to offer more Americans health-care coverage.

“Today’s U.S. Supreme Court decision is a major victory for all Americans benefiting from this groundbreaking and life-changing law,” Biden said in a statement. “It is a victory for more than 130 million Americans with preexisting conditions and millions more who were in immediate danger of losing their health care in the midst of a once-in-a-century pandemic.”

In a tweet, the president was more casual: “With millions of people relying on the Affordable Care Act for coverage, it remains, as ever, a BFD.” That was a reference to his slightly profane description when Obama signed the law in 2010, and Biden was vice president.

The court’s decision felt something like closure to a decade of attempts to overturn the law through the courts. Congress was never able to muster the votes to undo it even when Republicans controlled both chambers.

Former president Donald Trump had vowed to end the law legislatively, and through his nominees to the Supreme Court. But in Thursday’s decision, only one of his three nominees to the court said the law should fall.

The key issue this time was whether a 2017 decision by Congress to remove the penalty for not buying health insurance — known as the individual mandate — meant that the law was unconstitutional and should be wiped from the books.

That would end popular provisions such as keeping young adults on their parents’ insurance policies and ensuring coverage for those with preexisting medical conditions. Most legal analysts, both liberal and conservative, thought the lawsuit unconvincing.

The case posed three questions: Have the challengers — 18 states and a couple of individuals — suffered injuries that give them legal standing to bring the challenge? Did changes Congress made in 2017 render unconstitutional the ACA’s requirement for individuals to buy insurance? And if so, can the rest of the law be separated out, or must it fall in its entirety?

Breyer said that answering the first question negated the necessity of deciding the others.

“With the penalty zeroed out, the IRS can no longer seek a penalty from those who fail to comply,” Breyer wrote. So the individual plaintiffs face no government retribution for failing to purchase health insurance, he wrote.

And the states fare no better, he wrote. They “have not demonstrated that an unenforceable mandate will cause their residents to enroll in valuable benefits programs that they would otherwise forgo.”

Lawsuits require such a showing of “concrete, particularized injury” to justify intervention by courts, Breyer wrote. Otherwise, the law would grant “unelected judges a general authority to conduct oversight of decisions of the elected branches of government.”

His opinion was joined by Chief Justice John G. Roberts Jr. and Justices Clarence Thomas, Sonia Sotomayor, Elena Kagan, Brett M. Kavanaugh and Amy Coney Barrett.

Barrett and Kavanaugh were nominated by Trump. Barrett in particular was opposed by some Democrats who said Trump’s rush to have her take the place of the late Justice Ruth Bader Ginsburg was because of the ACA case. It was argued just after she joined the court last fall.

Roberts has earned the lasting enmity of some conservatives for voting to uphold the ACA in its original challenge; he assigned Thursday’s opinion to Breyer, the 82-year-old liberal who many progressives are urging to retire while Democrats control the White House and Congress.

But the vote was more lopsided than past ACA opinions because of Thomas. He has been a vigorous opponent of the ACA in previous challenges but said this one was ill-advised.

“This Court has gone to great lengths to rescue the Act from its own text,” he wrote of the previous decisions. But he added that Thursday’s result is “not the consequence of the Court once again rescuing the Act, but rather of us adjudicating the particular claims the plaintiffs chose to bring.”

Justice Samuel A. Alito Jr. and Justice Neil M. Gorsuch, Trump’s other nominee, dissented.

“Today’s decision is the third installment in our epic Affordable Care Act trilogy, and it follows the same pattern as installments one and two,” Alito wrote. “In all three episodes, with the Affordable Care Act facing a serious threat, the Court has pulled off an improbable rescue.”

He said the states had standing to sue.

“The ACA imposes many burdensome obligations on States in their capacity as employers, and the 18 States in question collectively have more than a million employees,” he wrote. “Even $1 in harm is enough to support standing. Yet no State has standing?”

Moreover, on the merits, Alito said he would find the entire law must fall.

“No one can fail to be impressed by the lengths to which this Court has been willing to go to defend the ACA against all threats,” he wrote.

Alito noted that originally the court had upheld the constitutionality of the law and the individual mandate because of Congress’s taxing power.

But now, “a tax that does not tax is allowed to stand and support one of the biggest Government programs in our Nation’s history,” he wrote. “Fans of judicial inventiveness will applaud once again. But I must respectfully dissent.”

Health and Human Services Secretary Xavier Becerra called the court’s decision “a victory for all Americans, especially people with a preexisting condition or anyone who was worried they could be forced to choose between their health and making ends meet.”

Before becoming the Biden administration’s top health official, Becerra was the California attorney general and led a coalition of Democratic state attorneys general who were the main group fighting to preserve the law as the case worked its way through federal courts.

It is part of a long legal battle.

As soon as the law was enacted, ACA opponents took their grievances into the federal courts.

In its first ruling, in 2012, the high court upheld the law, but the majority said the expansion of Medicaid could not take place nationwide as originally intended. Instead, the court said, it must be decided by each state. So far, three dozen states and the District of Columbia have expanded, and Congress and the Biden administration this year are offering new incentives for holdouts to join them.

In 2015, the Supreme Court again upheld a key portion of the law in a case that had challenged the federal subsidies the ACA provides most people who buy health plans through the insurance marketplaces.

The case involved in Thursday’s ruling began in early 2018, with Republican attorneys general. In an unorthodox move, Trump’s Justice Department said it would not defend the law.

A federal district judge struck down the law. A year later, a panel of the U.S. Court of Appeals for the 5th Circuit agreed that the law’s requirement that most Americans carry health insurance was unconstitutional, but sent back to the lower court the question of whether the rest of the law could remain without the mandate.

At that point, the Supreme Court stepped in.

The cases are California v. Texas and Texas v. California.

Amy Goldstein contributed to this report.