Speaker Paul D. Ryan on Tuesday in Washington. He reiterated Tuesday that he wants migrant families to be kept together.

WASHINGTON — A federal judge in California issued a nationwide injunction late Tuesday temporarily stopping the Trump administration from separating children from their parents at the border and ordered that all families already separated be reunited within 30 days.

Judge Dana M. Sabraw of the Federal District Court in San Diego said children under 5 must be reunited with their parents within 14 days, and he ordered that all children must be allowed to talk to their parents within 10 days.

“The unfortunate reality is that under the present system, migrant children are not accounted for with the same efficiency and accuracy as property,” the judge wrote.

Judge M. Sabraw’s order, which is likely to prompt a high-profile legal battle with the Justice Department, came on the same day that President Trump won a landmark legal victory when the Supreme Court upheld his travel ban, ending a 17-month legal fight.

But the judge’s ruling in the family separation case raises the stakes on an issue that had already become an intensely difficult political crisis for Mr. Trump. The president last week issued an executive order seeking to bring family separations to an end, but saying little about reuniting families.

The American Civil Liberties Union had filed a lawsuit to stop the separations before the president’s executive order. In his order, Judge Sabraw said that children may be separated at the border only if the adults with them present an immediate danger to the children.

He also said that adults may not be deported from the United States without their children.

“The facts set forth before the court portray reactive governance — responses to address a chaotic circumstance of the government’s own making,” the judge wrote in the opinion. “They belie measured and ordered governance, which is central to the concept of due process enshrined in our Constitution. This is particularly so in the treatment of migrants, many of whom are asylum seekers and small children.”

Read the Judge’s Order (PDF)

A federal judge in California issued a nationwide injunction temporarily stopping the Trump administration from separating families at the border, and he ordered that all families already separated be reunited within 30 days.

24 pages, 1.22 MB

In a statement, Lee Gelernt, the lead lawyer in the case for the A.C.L.U., hailed the judge’s order.

“This is an enormous win and will mean that this humanitarian crisis is coming to an end,” Mr. Gelernt said. “We hope the Trump administration will not think about appealing when the lives of these little children are at stake.”

The A.C.L.U. lawsuit was initially brought on behalf of two cases in which the children of immigrants were taken from them after they crossed the border.

In one case, a woman entered the United States legally at a port of entry, fleeing persecution in the Democratic Republic of the Congo. The lawsuit says that her 6-year-old daughter was taken from her and sent to a facility in Chicago, where she stayed for nearly five months.

In another case, a woman from Brazil, identified as “Ms. C,” crossed the border illegally with her 14-year-old son, who was separated from her and also sent to a facility in Chicago. The mother and son were apart for nearly eight months, according to the lawsuit.

“During the five months she was detained, Ms. C. did not see her son, and they spoke on the phone only ‘a handful of times,’” the lawsuit says. “Ms. C. was ‘desperate’ to be reunited with her son, worried about him constantly and did not know when she would be able to see him.”

Earlier Tuesday, seventeen states sued Mr. Trump for his administration’s practice of separating immigrant parents from their children, saying that the tactic is causing “devastating harm,” even as a top official said the government was struggling to reunite families fractured by the policy.

On a day when Mr. Trump basked publicly in the glow of a victory, with the Supreme Court upholding his travel ban, he faced a new legal challenge to what has emerged as the most controversial piece of his immigration agenda.

The states, including Washington, California and New York and joined by the District of Columbia, branded the forcible separation of immigrant families unconstitutional, “cruel and unlawful,” calling it a violation of the principles of due process and equal protection. They requested that the court halt it and immediately compel the government to reunite parents with their children.

The Trump administration says it is trying to do just that, but…