Washington (CNN) The Supreme Court said Thursday that federal courts must stay out of disputes over when politicians go too far in drawing district lines for partisan gain -- a dramatic and sweeping ruling that could fundamentally affect the balance of power in state legislatures and Congress.

The decision is all but certain to minimize the influence of minority parties in states where one party controls the governor's office and legislature, and where lawmakers are in charge of drawing district lines. Already, Democratic victories in lower courts against GOP-drawn maps in Michigan and Ohio are now on track to be undone.

Chief Justice John Roberts wrote the 5-4 decision for the conservative majority.

The court's nine justices issued rulings on two major cases during their final day of the term, both dealing with partisan politics. Roberts split his votes, crossing ideological lines in the other case to side with liberals on a decision that blocked a citizenship question from being added to the 2020 census for the time being. Excessive partisanship in districting leads to results that reasonably seem unjust. But the fact that such gerrymandering is 'incompatible with democratic principles' ... does not mean that the solution lies with the federal judiciary," he wrote in the gerrymandering opinion.

"We conclude that partisan gerrymandering claims present political questions beyond the reach of the federal courts," Roberts added. "Federal judges have no license to reallocate political power between the two major political parties, with no plausible grant of authority in the Constitution, and no legal standards to limit and direct their decisions."

Justice Elena Kagan read a scathing dissent from the bench for the four liberals.

"(G)errymandering is, as so many Justices have emphasized before, anti-democratic in the most profound sense," Kagan wrote.

"Of all times to abandon the Court's duty to declare the law, this was not the one," Kagan said. "The practices challenged in these cases imperil our system of government. Part of the Court's role in that system is to defend its foundations. None is more important than free and fair elections. With respect but deep sadness, I dissent."

Democrats have had success in races that tip the balance of state legislative chambers, and won a victory last year in Pennsylvania when the state's Supreme Court ruled its constitution prohibited gerrymandering. But Thursday's ruling dashed hopes for new limits nationwide on gerrymandering.

The ruling was a massive blow former Attorney General Eric Holder, who has led an effort -- with fundraising help from former President Barack Obama -- to fight GOP-drawn district maps. Holder said in a statement the court's decision "tears at the fabric of our democracy and puts the interests of the established few above the many."

"History will not be kind in its assessment of the ways in which this court has undermined voting rights and core democratic principles in America," Holder said.

Roberts said he believes this ruling does not mean there cannot be limits on partisan gerrymandering.

"Our conclusion does not condone excessive partisan gerrymandering," he wrote. "Nor does our conclusion condemn complaints about districting to echo into a void. The States, for example, are actively addressing the issue on a number of fronts."

The court was asked to consider when politicians go too far in drawing lines for partisan gain in a set of cases arising from North Carolina and Maryland.

"Today's ruling will have enormous downstream consequences across the American political map. In some states, it will favor Democrats; in more states, it will favor Republicans. But what it really favors is polarization, because primary elections will become more important than the general election for many seats in the House of Representatives -- and candidates usually have to run to their base in primaries," said Steve Vladeck, CNN Supreme Court analyst and professor at the University of Texas School of Law.

"And it's the exact move that Justice Anthony Kennedy refused to make in a series of cases over the course of his career," he said. "Here, as much as in any other decision thus far, we see the importance of Justice Brett Kavanaugh -- who signed onto the majority opinion -- as Kennedy's successor."

North Carolina

Rucho v. Common Cause was brought by voting rights groups and Democratic voters, among others, who argued that North Carolina's 2016 congressional districting plan was unconstitutional, saying the map drawn by Republican legislators amounted to an unconstitutional partisan gerrymander that intentionally diluted the electoral strength of individuals who oppose Republicans.

Allison Riggs of the Southern Coalition for Social Justice, who argued against the maps before the court, said in a statement on Thursday that the decision "is obviously a deeply disappointing outcome."

"We respectfully disagree with the Chief Justice that this decision won't be read as an endorsement of excessive partisan gerrymandering. Sadly, given our vast experience in redistricting, we can think of nothing else it will do," Riggs said in the statement.

Riggs had argued in court papers that the map drawers "divided clusters of Democrats that could have anchored congressional districts and submerged the fragments within larger masses of Republicans."

Riggs pointed out that Republicans won 53% of the vote in the 2016 election but they also won 10 of the state's 13 congressional seats.

Riggs had warned the justices that if they don't step in now, the use of politics will only get worse as map drawers rely increasingly on redistricting software that has become more sophisticated, adding that studies show the ideological gap between Democrats and Republicans in Congress is larger than ever before.

Maryland

The other case, Lamone v. Benisek, came from Maryland. A lower court had blocked the map, holding that individuals in the district were retaliated against based on how they had voted, in violation of the First Amendment.

This case was brought by seven Republican voters, who argued that then-Democratic Gov. Martin O'Malley, who was overseeing the redistricting process, took particular aim at the state's 6th congressional district.

Their lawyer, Michael Kimberly, also expressed disappointment with the court's ruling, saying in a statement on Thursday that it is "simply wrong to say that judging partisan gerrymanders is beyond the competence of federal courts."

"Partisan gerrymandering imperils our democracy. Its goal is to insulate those in power from accountability to the voting public. Nothing could be more undemocratic. We can't stand idly by and allow gerrymandering to continue, and we won't," Kimberly said in the statement.

The lawyer had argued in court papers that "map drawers methodically dismantled the sixth district, breaking apart large swaths of territory dominated by rural Republicans and replacing them with smaller, densely populated areas dominated by suburban Democrats."

The officials targeted some 66,000 Republicans in the district and added some 24,000 Democratic voters, therefore swinging the district, according to court documents.

The state's current governor, Republican Larry Hogan, said in a statement on Thursday that gerrymandering "stifles real political debate, contributes to our bitter partisan polarization, and deprives citizens of meaningful choices."

Hogan vowed to fight the issue next year in the state legislature.

This story has been updated.