The Supreme Court on Tuesday ruled in favor of the Trump administration by deciding federal officials can detain immigrants at any time for possible deportation after they have served their time in the U.S. for other crimes.

The 5-4 decision reversed the 9th Circuit Court of Appeals, which said officials have to detain these immigrants immediately or they are exempt from ever being detained.

Justice Samuel Alito delivered the majority opinion for the court, and he was joined by Chief Justice John Roberts and Justices Clarence Thomas, Neil Gorsuch, and Brett Kavanaugh. Justices Steven Breyer, Ruth Bader Ginsburg, Sonia Sotomayor, and Elena Kagan dissented.

Alito wrote that in the past, the court has "held time and time again, an official's crucial duties are better carried out late than never."

He also rejected the argument from lawyers representing the immigrants involved in the case that they are subject to mandatory detention only if they are arrested "on the day he walks out of jail," giving the example that state and local officials "sometimes rebuff the government's request that they give notice when a criminal alien will be released."

At the center of the case are immigrants Mony Preap and Bassam Yusuf Khoury, who are in the U.S. as lawful permanent residents. Both were convicted of crimes and served their sentences but were not detained by immigration authorities for removal proceedings until years after they were released from criminal custody.

The dispute focused on a federal law that says the Department of Homeland Security can detain immigrants convicted of certain crimes “when the alien is released” from criminal custody.

Lawyers for Preap and Khoury, as well as other immigrants in similar circumstances, argued they are exempt from mandatory detention because of the gap in their custody, as the statute applies only if the immigrant is taken into custody immediately upon release.

The 9th U.S. Circuit Court of Appeals agreed, and in a 2016 ruling, the appeals court said the word “when” in the statute “conveys immediacy,” and immigration detention has to occur promptly after the immigrant is released from criminal custody.

The Trump administration argued that the government has the authority to detain immigrants as they await deportation, even if they are arrested by immigration authorities years after serving their sentences. The Supreme Court agreed.

Reading his dissent from the bench, Breyer warned the "greater importance in the case lies in the power that the majority's interpretation grants to the government."

"It is a power to detain persons who committed a minor crime many years before. And it is a power to hold those persons, perhaps for many months, without any opportunity to obtain bail," he wrote.

[Also read: Illegal immigration hits 12-year-high in February]