Advocates had argued the law only allowed for detention immediately after immigrants were released from prison. The government can then hold them.
The San Francisco-based 9th US Circuit Court of Appeals, a liberal-leaning court Trump has slammed in other cases, ruled in 2016 that convicted immigrants who are not immediately detained by immigration authorities after finishing their sentences can not later be placed into indefinite detention awaiting possible deportation.
Once considered a proposal popular only among the political fringes, Trump's back-to-back appointments of Supreme Court justices Neil Gorsuch and Brett Kavanaugh revived "court packing" as a mainstream issue in response to Democrats' fears that conservatives could gain a majority on the court for decades to come.
The court's liberal justices dissented from the conservative decision.
"It is a power to detain persons who committed a minor crime many years before".
Associate Justice Stephen Breyer wrote the dissent for the court's liberals - and in a rare move, delivered it from the bench, United States media reported.
He wrote that in his view the law requires immigrants who have committed crimes to be detained "within a reasonable time after their release" from custody, "presumptively no more than six months".
For people in the Washington area, the shooting spree is remembered as particularly frightening because of the randomness of the attacks.
Ten states had urged the Supreme Court to take the case. Wang also called the decision an "extreme waste of taxpayer money", saying it locks up individuals who are not a danger to the community.
The U.S. Court of Appeals for the 9th Circuit said that meant immediately upon release from custody.
The plaintiffs included two legal US residents involved in separate lawsuits filed in 2013, a Cambodian immigrant named Mony Preap convicted of marijuana possession and a Palestinian immigrant named Bassam Yusuf Khoury convicted of attempting to manufacture a controlled substance.
The case turned on a phrase in the law that says the no-bail determination applies to someone picked up by ICE "when the alien is released" from prison or jail.
The case is 16-1363 Nielsen v. Preap.