WASHINGTON — The U.S. Supreme Court dealt a crippling blow Wednesday to unions representing millions of the nation's public employees.

The justices said in a 5-4 opinion that state government workers who choose not to join a union cannot be compelled to pay a share of union dues for covering the cost of negotiating contracts. Unions had said such an outcome would cut off a source of income and diminish their political clout in the 23 states where they bargain for both members and nonmembers alike.

A recent nonpartisan study predicted that a Supreme Court defeat would eventually cause public employee unions to lose 726,000 members, a significant blow in one of organized labor's remaining strongholds. Nearly half of all union members in the U.S. are government employees.

President Donald Trump tweeted shortly after the ruling that it was a "Big loss of the coffers of the Democrats!"

Supreme Court rules in favor of non-union workers who are now, as an example, able to support a candidate of his or her choice without having those who control the Union deciding for them. Big loss for the coffers of the Democrats! — Donald J. Trump (@realDonaldTrump) June 27, 2018

The court has long held that requiring nonunion members to pay the full amount of union dues would violate their right of free expression, forcing them to subsidize a union's political activities whether they agree with its goals or not.

But in 1977, the Court said nonunion employees could be required to pay a portion of union dues, known as agency fees, to cover the cost of collective bargaining and prevent "free riders" — workers who get the benefits of a union contract without paying for it.

Wednesday's ruling overturned that earlier decision.

"States and public-sector unions may no longer extract agency fees from nonconsenting employees," Justice Samuel Alito said in his majority opinion for the court's five conservative justices.

Alito noted that unions could "experience unpleasant transition costs in the short term," but he said labor's problems pale in comparison to "the considerable windfall that unions have received ... for the past 41 years."

Billions of dollars have been taken from workers who were not union members in that time, which Alito said was unconstitutional and "cannot be allowed to continue indefinitely."

Justice Elena Kagan wrote in her dissent, joined by the court's three other liberals, of the big impact of the decision.

"There is no sugarcoating today's opinion. The majority overthrows a decision entrenched in this nation's law — and its economic life — for over 40 years," Kagan said. "As a result, it prevents the American people, acting through their state and local officials, from making important choices about workplace governance. And it does so by weaponizing the First Amendment, in a way that unleashes judges, now and in the future, to intervene in economic and regulatory policy."

The current challenge began with a lawsuit against an Illinois public sector union filed by Gov. Bruce Rauner, a Republican. He was eventually dropped from the litigation, but a state child support specialist, Mark Janus, carried on the fight. He claimed that the dues requirement violated his freedom of association, arguing that even the positions unions take in collective bargaining are essentially political because the quality of public services is at stake.

The court deadlocked 4-4 on the same issue two years ago in a case brought by a group of California teachers opposed to paying the dues. Since then conservative Neil Gorsuch joined the court, providing the fifth vote opponents needed to strike the fees down.

"By requiring unions to represent everyone in a collective bargaining unit without ensuring fair contributions for that representation, unions will be forced to do more with much less — to the detriment of those they represent," said Rep. Linda Sanchez, D-Calif., an International Brotherhood of Electrical Workers union member and former union lawyer. "The decision today is the latest blow in a decadeslong, nationwide effort by conservatives to bust unions that advocate for workers and protect them from employer abuses."