WASHINGTON—The Supreme Court dealt a major blow to Donald Trump’s prosecution on charges he sought to subvert the 2020 election, ruling 6-3 Monday that former presidents enjoy sweeping immunity for their acts while in office.

The president “may not be prosecuted for exercising his core constitutional powers, and he is entitled, at a minimum, to a presumptive immunity from prosecution for all his official acts,” Chief Justice John Roberts wrote for the court, joined in whole or part by Justices Clarence Thomas, Samuel Alito, Neil Gorsuch, Brett Kavanaugh and Amy Coney Barrett.

But “the President enjoys no immunity for unofficial acts, and not everything the President does is official,” Roberts wrote.

The court on its own threw out parts of the prosecution’s case against Trump, including on his alleged efforts to use the Justice Department to advance his unsubstantiated claims of election fraud and submit slates of false electors to replace those President Biden won. “The President may discuss potential investigations and prosecutions with his Attorney General and other Justice Department officials to carry out his constitutional duty,” Roberts wrote.

“Trump is therefore absolutely immune from prosecution for the alleged conduct involving his discussions with Justice Department officials,” including his threat to remove acting Attorney General Jeffrey Rosen, Roberts wrote.

For other allegations, the court instructed the trial judge to review Trump’s indictment for charges that must be dismissed because they are based on his official acts, apparently before proceedings could begin on whatever remains, if anything, of the case.

The liberal dissenters could barely contain their outrage. The decision “makes a mockery of the principle, foundational to our Constitution and system of Government, that no man is above the law,” Justice Sonia Sotomayor wrote, joined by Justices Elena Kagan and Ketanji Brown Jackson.

“With fear for our democracy, I dissent,” she wrote.

Nadine Seiler holds a banner outside the U.S. Supreme Court, following U.S. Supreme Court justices rule on former U.S. President and Republican presidential candidate Donald Trump’s bid for immunity from federal prosecution for 2020 election subversion in Washington, U.S., July 1, 2024. REUTERS/Leah Millis

The decision effectively gave Trump nearly everything he could have hoped for, a victory he quickly celebrated.

“Big win for our Constitution and democracy. Proud to be an American!” Trump, using all capital letters, wrote on his social-media platform.

Even for parts of the case that remain, the court added so many new conditions for prosecutors that it would likely be virtually impossible to hold a trial before Election Day. And the chief justice’s opinion erects new hurdles for federal prosecutors that may be difficult to clear. For example, the court said that, when attempting to distinguish Trump’s official acts from unofficial ones, judges can’t inquire into Trump’s motives. And he said prosecutors can’t use Trump’s official acts as evidence to support claims that he committed a crime with unofficial ones.

Roberts also said Trump was “presumptively immune” for his alleged attempts to pressure Vice President Mike Pence, who presided over the congressional meeting to certify the election, to reject Biden electors. To proceed on those allegations, prosecutors must persuade the trial court that so doing would not “pose any dangers of intrusion on the authority and functions of the Executive Branch,” he said.

The court previously has handed Trump victories in two separate cases stemming from his followers’ Jan. 6, 2021, attack on the U.S. Capitol, where they sought to stop Congress from certifying President Biden’s victory.

On Friday, the court narrowed the scope of an obstruction charge federal prosecutors have filed against Jan. 6 rioters as well as Trump himself. The justices found that the offense was limited to interference with documents and other things required by an official proceeding—not obstructing an actual meeting of Congress. Lower courts will have to sort out the opinion’s impact on the Jan. 6 prosecutions.

That followed the court’s March decision restoring Trump’s eligibility for the Colorado ballot, after that state’s highest court disqualified the Republican candidate under a constitutional provision that bars former officeholders who engaged in insurrection or rebellion from future office. The Supreme Court said that states lacked authority to enforce the Reconstruction-era clause against federal candidates.

While Monday’s decision is sure to color perceptions of Trump’s potential criminality, it is also bound to affect public opinion of the court. Three Trump appointees sit on the nine-member court, where their votes have cemented historic victories for long-sought conservative goals, from the 2022 decision overruling Roe v. Wade to another ruling Friday that overturned a Reagan-era precedent requiring judicial deference to executive-branch agencies when the law is ambiguous.

But no case to date has put Trump’s personal interests so directly in the hands of the justices he appointed—and from whom he has expected a sympathetic hearing. Moreover, two other justices have familial ties to Trump’s cause—Justice Clarence Thomas’s wife, Ginni Thomas, urged White House officials to take steps to block Biden from taking office; Justice Samuel Alito’s wife, Martha-Ann Alito, flew flags at the couple’s homes like those carried by Jan. 6 rioters, the justice has said.

However ironclad the legal rationales behind their votes, the justices’ actions cannot avoid being viewed in the context of such connections.

Trump has been indicted in four different cases, two at the federal level by special counsel Jack Smith. The case before the high court involved perhaps the most serious charges: allegations by Smith that Trump participated in an illegal scheme whose aim was to deny Biden the victory he won on Election Day 2020.

Under a Justice Department policy, sitting presidents can’t be prosecuted for violating federal law, while a 1981 Supreme Court precedent held that former presidents can’t be sued by private parties over their official acts while in office. Trump, who denies wrongdoing across the board, argued that similar immunity should insulate him from standing trial for crimes he allegedly committed as president.

By pursuing the immunity argument, Trump derailed, at least temporarily, the spectacle of trial in federal court. The appeal froze the prosecution and forced U.S. District Judge Tanya Chutkan to scratch a trial that had been scheduled to begin March 4.

At the Supreme Court, Trump’s lawyers argued that without immunity for their official acts, presidents would be hamstrung from taking bold action in the national interest for fear that such decisions could be prosecuted as crimes by subsequent administrations. The special counsel contended that such fears were overblown because of internal safeguards within the Justice Department and the independent check on prosecutorial overreach that grand juries and federal courts provide.

Both sides pointed to the unprecedented nature of the Trump prosecution for support: The ex-president argued that Smith had broken a tradition of not charging former presidents with crimes. The special counsel countered that no president who lost re-election before had broken the law to block the transfer of power.

The charges against Trump, handed up by a federal grand jury in August 2023, include using dishonesty and fraud to obstruct the electoral process and conspiring to deny citizens’ rights to have their votes counted.

Chutkan rejected Trump’s immunity claim in December, a decision upheld in February by the U.S. Court of Appeals for the District of Columbia Circuit. From the start, however, Trump’s attorneys placed hope in the Supreme Court.

Trump’s team contended that the most serious allegations he faces, including pressuring state officials and Vice President Pence to participate in the scheme to retain power, should be classified as official actions beyond the reach of the law.

At oral arguments in April, justices in the court’s conservative majority said little about the allegations against Trump. The hearing instead focused on speculative risks to future presidents from bad-faith prosecutors who theoretically could bedevil a former chief executive with partisan grudges dressed up as criminal charges.

Should Trump prevail in November, upon return to the White House he could order the federal charges against him withdrawn or attempt to pardon himself for past crimes, ending both the Washington case and separate charges Smith filed in Florida alleging Trump illegally took national-security documents from the White House when leaving office in 2021 and refused to turn them over when requested by federal officials. State proceedings against Trump could also freeze, based on the constitutional supremacy of federal law over those enforced by the states.

Write to Jess Bravin at Jess.Bravin@wsj.com