The court seemed to tip its hand at oral arguments in April, when a majority of the justices appeared ready to side with Trump. The court was ruling on what was the third version of the ban, which Trump has complained is a "watered-down" version.
The court allowed it to go into effect while the case was being litigated, but the lower courts had ruled that all three versions either violate federal law or are unconstitutional.
Like the earlier two bans, version 3.0 bars almost all travelers from five mainly Muslim countries — Iran, Syria, Yemen, Libya and Somalia — and added a ban on travelers from North Korea and government officials from Venezuela.
The court acceded broadly to presidential power. The majority opinion, written by Chief Justice John Roberts, noted that the INA exudes deference to the president. The executive order, he wrote, was more detailed than similar orders by Presidents Ronald Reagan and Jimmy Carter.
Roberts then deferred to the president's power. The only thing a president has to signal is that entry for people from various countries would be detrimental to the interest of the United States. The president undoubtedly fulfilled that requirement here, the court noted.
The president, Roberts said, has extraordinary power to express his opinions to the country as well. The plaintiffs argue that Trump's past campaign and other statements about Muslims should be taken into account, but the majority said it is not the court's role to do that.
"The issue, however, is not whether to denounce the President's statements," Roberts said, "but the significance of those statements in reviewing a Presidential directive, neutral on its face, addressing a matter within the core of executive responsibility. In doing so, the Court must consider not only the statements of a particular President, but also the authority of the Presidency itself."
Justices Stephen Breyer and Sonia Sotomayor both made the relatively rare decision to read their dissents from the bench.
Speaking with unusual passion, Sotomayor blasted the court's reasoning.
"The United States of America is a Nation built upon the promise of religious liberty," she opened in her dissent. "Our Founders honored that core promise by embedding the principle of religious neutrality in the First Amendment. The Court's decision today fails to safeguard that fundamental principle."
What's more, Sotomayor said, the court's decision "leaves undisturbed a policy first advertised openly and unequivocally as a 'total and complete shutdown of Muslims entering the United States' because the policy now masquerades behind a façade of national-security concerns."
Comparing Tuesday's outcome with the court's decision in 1944 to uphold the legality of Japanese-American internment camps, Sotomayor noted, "In holding that the First Amendment gives way to an executive policy that a reasonable observer would view as motivated by animus against Muslims, the majority opinion upends this Court's precedent, repeats tragic mistakes of the past, and denies countless individuals the fundamental right of religious liberty."
She added that plaintiffs did have a case related to the establishment clause of the Constitution and blasted the majority for believing otherwise.
"The majority holds otherwise by ignoring the facts, misconstruing our legal precedent, and turning a blind eye to the pain and suffering the Proclamation inflicts upon countless families and individuals, many of whom are United States citizens," Sotomayor added.
And while the court has a duty to be deferential to the president, she said, "Deference is different from unquestioning acceptance. Thus, what is 'far more problematic' in this case is the majority's apparent willingness to throw the Establishment Clause out the window and forgo any meaningful constitutional review at the mere mention of a national-security concern."
She then read a selection of anti-Muslim statements made by the president, admonishing the audience to "take a brief moment to let that sink in."
After reading her decision from the bench, she added, "History will not look kindly on the court's decision today — nor should it."
Outside reaction to the ruling was swift.
"This ruling will go down in history as one of the Supreme Court's great failures," said Omar Jadwat, director of the ACLU's Immigrants' Rights Project, in a statement. "It repeats the mistakes of the Korematsu decision upholding Japanese-American imprisonment and swallows wholesale government lawyers' flimsy national security excuse for the ban instead of taking seriously the president's own explanation for his action."
House Democratic leader Nancy Pelosi slammed the decision in a statement, saying, "The court failed today, and so the public is needed more than ever" to challenge officials who do not move to rescind the ban.
Pelosi added that the ban will actually backfire and serve as a "recruiting tool" for terrorists, and she invoked other recent controversial foreign policy moves by Trump.
"The President's disdain for our values and the safety of the American people has led him to undermine relationships with critical allies, embrace autocrats and dictators, launch damaging trade wars and sow fear in our communities with his hateful, ugly language," she said. "Whether tearing children from their parents at the border or advancing a ban founded on open bigotry, President Trump is making our nation less safe at home and less respected abroad."