Washington (CNN) -- The Supreme Court has affirmed a federal order telling California to reduce its overflowing prison population, a situation the majority said "falls below the standard of decency."

The 5-4 ruling Monday from the justices come in a classic battle over state versus federal authority, focusing on whether U.S. courts can step in and essentially run state prisons when officials have repeatedly violated basic constitutional guarantees afforded inmates.

The issue came down to a sharply divided debate between public safety concerns and individual rights, a debate that goes into how the three branches of government should balance competing state interests.

The swing vote was Justice Anthony Kennedy, who wrote of the "continuing injury and harm resulting from these serious constitutional violations," including as many as 156,000 people crammed in correctional facilities designed to hold about half that many.

He noted "needless suffering and death have been the well-documented result. Over the whole course of years during which this litigation has been pending, no other remedies have been found to be sufficient."

In dissent, Justice Samuel Alito warned any mass release of inmates to alleviate overcrowding would be "gambling with the safety of the people of California."

The state now has a two-year window to comply, with the clock starting Monday. Officials have not fully explained how their ongoing inmate reduction plan will need to be modified to meet the federal order.

In a statement issued Monday afternoon, California Gov. Jerry Brown said he would take "all steps necessary to protect public safety" in implementing the order.

Brown, a Democrat, signed legislation in April that was aimed at cutting the state's prison population by tens of thousands, largely by housing non-violent felons with sentences less than three years in county jails. In his Monday statement, Brown said the court's ruling recognized that law as "key to meeting this obligation," but said the plan still needs "full and constitutionally guaranteed funding."

Prison overcrowding is a nationwide problem, but California's dilemma is unique in its massive scope and time frame. There is general agreement that the prison conditions across California are disturbing.

Prisoners are stacked three deep in 6-by-9-foot cells designed to hold only one. Open spaces meant to be gymnasiums and clinics have been transformed into crowded encampments with bunks and unsanitary conditions. Suicides occur once every eight days on average.

California has the nation's largest prison system, and the state says it has reduced the prison population to meet overcrowding concerns. But a special federal court panel had ordered 36,000 to 46,000 more inmates released or transferred quickly, about a quarter of the total.

Despite some recent drops, the prison population in the state has increased by about 75 percent in the past two decades.

Two lawsuits -- one filed in 1990, the other in 2001 -- say overcrowding is the core cause of what has become a domino effect of unsafe and unhealthy conditions for those on both sides of the iron bars.

State legislators and corrections officials have admitted the prisons violate the ban on "cruel and unusual punishment" contained in the Constitution, and have organized more than 20 panels and commissions to address the crisis.

Former Gov. Arnold Schwarzenegger, who left office in January, had blamed the legislature for not approving more money to build new prisons, or reforming the way defendants are punished and sentenced, particular repeat offenders.

"I don't blame the courts for stepping in to try to solve the overcrowding crisis," he said three years ago. "The fact of the matter is, for decades the state of California hasn't really taken it seriously and hasn't really done something about it."

The special federal court in 2009 had ordered the state to shrink the prison population from the current 200% over capacity to a maximum of 137.5%, and to accomplish that in two years. The state was given wide latitude to meet the goal, but the court was adamant the state do it without delay and without excuse.

The task was made more difficult by the state budget crisis and a national economic downturn that has created turmoil over funding solutions not just in prisons, but also in education, transportation, and social programs.

Kennedy spent most his 52-page majority opinion affirming the right of federal courts to step into the situation.

"This extensive and ongoing constitutional violation requires a remedy, and a remedy will not be achieved without a reduction in overcrowding," he wrote. "The relief ordered by the three-judge court is required by the Constitution and was authorized by Congress in (federal law). The state shall implement the order without further delay."

The state has already begun to comply; about 9,000 inmates have been released since the 2009 trial stemming from the lawsuits.

Justices Ruth Bader Ginsburg, Stephen Breyer, Sonia Sotomayor and Elena Kagan supported Kennedy's conclusions.

Two tough dissents followed the majority's ruling. Reading from the bench, Justice Antonin Scalia said the ruling represents "the most radical injunction issued by a court in our nation's history." He said it "takes federal courts wildly beyond their institutional capacity." Justice Clarence Thomas backed him.

In a separate dissent, Alito spoke of the potential impact of the decision.

"The prisoner release ordered in this case is unprecedented, improvident, and contrary" to federal law, he said. "I fear that today's decision, like prior prisoner release orders, will lead to a grim roster of victims. I hope that I am wrong. In a few years we will see."

Chief Justice John Roberts added his support to Alito's dissent.

The case is Brown v. Plata (09-1233).