WASHINGTON — A federal judge in Texas struck down the entire Affordable Care Act on Friday on the grounds that its mandate requiring people to buy health insurance is unconstitutional and the rest of the law cannot stand without it.

The ruling was over a lawsuit filed this year by a group of Republican governors and state attorneys general. A group of intervening states led by Democrats promised to appeal the decision, which will most likely not have any immediate effect. But it will almost certainly make its way to the Supreme Court, threatening the survival of the landmark health law and, with it, health coverage for millions of Americans, protections for people with pre-existing conditions and much more.

In his ruling, Judge Reed O’Connor of the Federal District Court in Fort Worth said that the individual mandate requiring people to have health insurance “can no longer be sustained as an exercise of Congress’s tax power.”

Accordingly, Judge O’Connor, a George W. Bush appointee, said that “the individual mandate is unconstitutional” and the remaining provisions of the Affordable Care Act are invalid.

When the Supreme Court upheld the mandate as constitutional in 2012, it was based on Congress’s taxing power. Congress, the court said, could legally impose a tax penalty on people who do not have health insurance.

But in the new case, the 20 plaintiff states, led by Texas, argued that with the penalty zeroed out, the individual mandate had become unconstitutional — and that the rest of the law could not be severed from it.

The Justice Department’s response to the case was highly unusual: though it disagreed with the plaintiffs that the entire law should be struck down, it declined this year to defend not just the individual mandate, but the law’s provisions that protect people with pre-existing conditions. That prompted a coalition of 16 states and the District of Columbia, led by California, to intervene and defend the law.

On Friday night, a spokeswoman for Xavier Becerra, the California attorney general, said California and the other defendant states would challenge the ruling with an appeal in the United States Court of Appeals for the Fifth Circuit in New Orleans.

“Today’s ruling is an assault on 133 million Americans with pre-existing conditions, on the 20 million Americans who rely on the A.C.A.’s consumer protections for health care, on America’s faithful progress toward affordable health care for all Americans,” Mr. Becerra said in a statement. “The A.C.A. has already survived more than 70 unsuccessful repeal attempts and withstood scrutiny in the Supreme Court.”

Attorney General Ken Paxton of Texas, who initiated the lawsuit, applauded the decision, saying in a statement, “Today’s ruling enjoining Obamacare halts an unconstitutional exertion of federal power over the American health care system.”

He added, “Our lawsuit seeks to effectively repeal Obamacare, which will give President Trump and Congress the opportunity to replace the failed social experiment with a plan that ensures Texans and all Americans will again have greater choice about what health coverage they need and who will be their doctor.”

Mr. Trump, who has consistently sought the law’s repeal and has weakened it through regulatory changes, posted a response to the ruling on Twitter late Friday night: “As I predicted all along, Obamacare has been struck down as an UNCONSTITUTIONAL disaster! Now Congress must pass a STRONG law that provides GREAT healthcare and protects pre-existing conditions.”

The White House, in a separate statement late Friday, said: “We expect this ruling will be appealed to the Supreme Court. Pending the appeal process, the law remains in place.”

If Judge O’Connor’s decision ultimately stands, about 17 million Americans will lose their health insurance, according to the Urban Institute, a left-leaning think tank. That includes millions who gained coverage through the law’s expansion of Medicaid, and millions more who receive subsidized private insurance through the law’s online marketplaces.

Insurers will also no longer have to cover young adults up to age 26 under their parents’ plans; annual and lifetime limits on coverage will again be permitted; and there will be no cap on out-of-pocket costs.

Also gone will be the law’s popular protections for people with pre-existing conditions, which became a major talking point in the November midterm elections, as Democratic candidates constantly reminded voters that congressional Republicans had tried to repeal the law last year.

Senator Joe Manchin III of West Virginia, one of the few Democratic senators to save his seat in a heavily Trump-friendly state, excoriated his opponent, West Virginia’s attorney general, Patrick Morrisey, for joining the lawsuit decided in favor of the Republicans on Friday.

Democrats in Wisconsin campaigned on a pledge to withdraw the state from the suit, and after they won, that state’s Republican Legislature passed legislation blocking the newly elected Democrats from withdrawing. That legislation was signed into law on Friday by Gov. Scott Walker, who lost his bid for re-election in November.

But most Republican candidates insisted during the campaign that they did not want to withdraw protections for pre-existing conditions, and most were silent after Friday’s ruling.

Without those protections, insurers could return to denying coverage to such people or to charging them more. They could also return to charging people more based on their age, gender or profession.

The Kaiser Family Foundation, a nonpartisan research organization, estimates that 52 million adults from 18 to 64, or 27 percent of that population, would be rejected for coverage under the practices that were in effect in most states before the Affordable Care Act.

“If this Texas decision on the ACA is upheld, it would throw the individual insurance market and the whole health care system into complete chaos,” Larry Levitt, a senior vice president of the Kaiser Family Foundation, wrote on Twitter. “But, the case still has a long legal road to travel before that’s an immediate threat.”

Democrats immediately attacked the ruling as absurd. Representative Nancy Pelosi of California said that when the party took control of the House next month, with her as speaker, it would “move swiftly to formally intervene in the appeals process to uphold the lifesaving protections for people with pre-existing conditions and reject Republicans’ effort to destroy the Affordable Care Act.”

In his ruling, Judge O’Connor agreed with the plaintiffs that the individual mandate could not be severed from the rest of the Affordable Care Act because it was “the keystone” of the law, essential to its regulation of the health insurance market.

“The individual mandate is inseverable from the entire A.C.A.,” he declared.

The judge said he would not “parse the A.C.A.’s provisions one by one,” but had to invalidate the whole law, including the expansion of Medicaid and the requirement for employers to offer coverage to workers. “The Medicaid-expansion provisions were designed to serve and assist fulfillment of the individual mandate,” he wrote.

At oral arguments before Judge O’Connor in September, California and the other intervening states had argued that the mandate could not be unconstitutional if it was not forcing people to pay penalties anymore. But even if Judge O’Connor threw it out, they said, the rest of the law could legally be severed from it and survive.

The ruling came a day before the end of the fifth open enrollment season for Affordable Care Act coverage in most states, one in which sign-ups for “Obamacare,” as the coverage sold through the law’s marketplaces is known, have declined so far by about 12 percent compared with last year.