WASHINGTON — A divided Supreme Court ruled Monday that most immigrant children who have become adults during their parents' years-long wait to become legal permanent residents of the United States should go to the back of the line in their own wait for visas.

In a 5-4 decision, the justices sided with the Obama administration in finding that immigration laws offer relief only to a tiny percentage of children who "age out" of the system when they turn 21. The majority — tens of thousands of children— no longer qualify for the immigration status granted to minors.

The case is unusual in that it pitted the administration against immigration reform advocates who said government officials were misreading a law intended to keep families together by preventing added delays for children seeking visas.

The ruling also features President Barack Obama's two court appointees — Justices Elena Kagan and Sonia Sotomayor — on opposing sides of a complicated debate over what the law means and what Congress intended when it wrote the laws governing wait times for children seeking visas.

The case does not have any impact on the recent influx of thousands of immigrant children traveling on their own to cross the U.S. border from Mexico.

Five justices agreed with the outcome of the case but there was no majority opinion. Writing for three justices, Kagan said the law preserves the place in line for a child whose petition for a visa was filed directly by a parent who is a green card holder, but not for children in other categories. She was joined by Justices Anthony Kennedy and Ruth Bader Ginsburg.

Chief Justice John Roberts and Justice Antonin Scalia wrote a separate opinion agreeing only with the outcome, but not Kagan's reasoning.

Because approving families for green cards can take years, tens of thousands of immigrant children age out of the system each year, according to government estimates. Congress tried to fix the problem in 2002 when it passed the Child Status Protection Act. The law allows aged-out children to retain their child status longer or qualify for a valid adult category and keep their place in line.

But appeals courts have split over whether the law applies to all children or only those in specific categories. The Obama administration argued that the law applied only to a narrow category of immigrants, leaving out most of the children affected. Government attorneys said that applying the law too broadly would lead to too many young adults entering the country ahead of others waiting in line.

Immigration advocates assert that the law was passed to promote family unity. According to Catholic Legal Immigration Network, forcing an aged-out child to go back to the end of the line would increase his or her wait time by more than nine years. By contrast, it says keeping the child's priority dates would increase the wait time for others by just a few months.

The case involved Rosalina Cuellar de Osorio, a Salvadoran immigrant who was in line for a visa along with her 13-year-old son. But after years of waiting, her son turned 21 and government officials said he no longer qualified as an eligible child. He was placed at the back of the line, resulting in a wait of several more years.

The family won its challenge before the 9th U.S. Circuit Court of Appeals, but the Supreme Court reversed that decision.

In a lengthy dissent, Sotomayor said there is no conflict in the law and it should be read to clearly allow all aged-out children to keep their place in line. She quotes a book by Scalia in which he says courts "do not lightly presume that Congress has legislated in self-contradicting terms."

Sotomayor was joined in dissent by Justices Stephen Breyer, Clarence Thomas. Justice Samuel Alito wrote a separate dissent.

A group of lawmakers in Congress when the law was passed — including Sens. John McCain, R-Ariz., Orrin Hatch, R-Utah, Chuck Schumer, D-N.Y. and Dianne Feinstein, D-Calif. — submitted a brief arguing against the government in the case.

Immigration reform groups were hoping the issue would be addressed in Congress. The Senate last year passed a bipartisan bill that would tighten border security, provide enforcement measures and offer a path to citizenship for the estimated 11 million immigrants living in the United States illegally. That measure stalled in the House, where Republicans have rejected a comprehensive approach in favor of a bill-by-bill process.