Three members of the court did not participate in the decision: Justice Neil M. Gorsuch, who had not yet joined the court when the case was argued in January, and Justices Sonia Sotomayor and Elena Kagan, who recused themselves.

They did not say why, but Justice Sotomayor used to be a judge on the appeals court that heard the case, and Justice Kagan formerly served as United States solicitor general. They had presumably been involved in the case in those capacities.

Justice Stephen G. Breyer summarized his dissent from the bench, a sign of deep disagreement.

“History tells us of far too many instances where the executive or legislative branch took actions during time of war that, on later examination, turned out unnecessarily and unreasonably to have deprived American citizens of basic constitutional rights,” he said in his written dissent, which canvassed several dark chapters in American history.

“We have read about the Alien and Sedition Acts, the thousands of civilians imprisoned during the Civil War, and the suppression of civil liberties during World War I,” he wrote. “The pages of the U.S. Reports” — which collect Supreme Court decisions — “themselves recite this court’s refusal to set aside the government’s World War II action removing more than 70,000 American citizens of Japanese origin from their West Coast homes and interning them in camps — an action that at least some officials knew at the time was unnecessary.”

Justice Breyer said that suits for money were a good way to check executive misconduct.

“In such circumstances,” he wrote, “courts have more time to exercise such judicial virtues as calm reflection and dispassionate application of the law to the facts. We have applied the Constitution to actions taken during periods of war and national-security emergency.”

Justice Ruth Bader Ginsburg joined Justice Breyer’s dissent.

The majority effectively dismissed the lawsuit against Mr. Ashcroft, Mr. Mueller and James W. Ziglar, a former commissioner for the Immigration and Naturalization Service. The Supreme Court returned part of the case, concerning the detention center’s warden, Dennis Hasty, to the United States Court of Appeals for the Second Circuit, in New York, for further consideration.

In general, Justice Kennedy wrote, there were competing interests in the case.

“The stakes on both sides of the argument are far higher than in past cases the court has considered,” Justice Kennedy wrote.