The Washington Post's Supreme Court reporter Robert Barnes explains the justices' 5-4 decision June 26 to uphold President Trump's travel ban. (Video: Joyce Koh/The Washington Post)

The Supreme Court ruled Tuesday that President Trump has the authority to ban travelers from certain majority-Muslim countries if he thinks it is necessary to protect the United States, a victory in what has been a priority since Trump’s first weeks in office and a major affirmation of presidential power.

The vote was 5 to 4, with conservatives in the majority and Chief Justice John G. Roberts Jr. finding that a string of unprecedented comments and warnings from Trump about Muslims did not erode the president’s vast powers to control entry into this country.

The president reacted on Twitter: “SUPREME COURT UPHOLDS TRUMP TRAVEL BAN. Wow!”

Later, the White House issued a formal response that also took a swipe at Trump’s declared enemies. It called the ruling a “vindication following months of hysterical commentary from the media and Democratic politicians who refuse to do what it takes to secure our border and our country.”

As the nation marks President Trump’s 100 days, see how these Syrian refugees once caught in the travel ban are finding their new home. (Video: Dalton Bennett/The Washington Post, Photo: Dalton Bennett/The Washington Post)

Lower courts had struck down each of the three iterations of the president’s travel ban, the first of which was issued in January 2017. But the administration said it fortified the order in response to each judicial setback, and it had reason to be optimistic about the Supreme Court, since the justices previously decided to let the ban go into effect while considering the challenges to it.

The Supreme Court’s ruling that Trump’s travel ban isn’t a ‘Muslim ban,’ annotated

The ruling was one of a string of 5-to-4 decisions this term in which the justices on the right reasserted themselves, after the addition of Trump-nominated Justice Neil M. Gorsuch last year restored a conservative majority.

The campaign of Senate Majority Leader Mitch McConnell (R-Ky.), who for 10 months kept the Republican-controlled Senate from voting on President Barack Obama’s nominee to the court after the death of Justice Antonin Scalia in 2016, celebrated by posting a picture on Twitter.

It was of him shaking hands with Gorsuch.

The current ban, issued last fall, barred various travelers from eight countries, six of them with Muslim majorities. They are Syria, Libya, Iran, Yemen, Chad, Somalia, North Korea and Venezuela. Restrictions on North Korea and Venezuela were not part of the challenge. Chad was later removed from the list.

Roberts tried to play down the political aspects of the case, writing that the proclamation that led to the ban “is squarely within the scope of Presidential authority” and noting that its text does not mention religion.

His opinion gave a short history of Trump’s comments about Muslims, starting with a campaign pledge for a “total and complete shutdown of Muslims entering the United States until our country’s representatives can figure out what is going on.” The pledge remained on the campaign website after Trump became president.

And other tweets and statements followed.

“But the issue before us is not whether to denounce the statements,” Roberts wrote. “It is instead 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, we must consider not only the statements of a particular President, but also the authority of the Presidency itself.”

He added: “We express no view on the soundness of the policy.”

Justice Sonia Sotomayor wrote a stinging rebuttal, joined by Justice Ruth Bader Ginsburg. And she read part of it in a dramatic moment on the bench.

Sotomayor noted the campaign statements and anti-Muslim videos and comments the president shared on Twitter, including one titled “Muslim Destroys a Statue of Virgin Mary!”

“Take a brief moment and let the gravity of those statements sink in,” she said.

“And then remember,” Sotomayor added, that the statements and tweets were spoken or written “by the current president of the United States.”

Sotomayor repeatedly called out Trump by name in her lengthy statement and said the majority’s decision “repeats tragic mistakes of the past” and “tells members of minority religions” in the United States that “they are outsiders.”

The court, she wrote, was “blindly accepting the Government’s misguided invitation to sanction a discriminatory policy.”

In her passionate dissent, Sotomayor compared the decision to Korematsu v. United States, in which the Supreme Court in 1944 upheld the detention of Japanese Americans during World War II. Both rulings, she said, will be considered historic mistakes.

Roberts objected. “Whatever rhetorical advantage the dissent may see in doing so, Korematsu has nothing to do with this case,” he wrote, adding that “it is wholly inapt to liken that morally repugnant order to a facially neutral policy denying certain foreign nationals the privilege of admission.”

But he said the reference did give the court a chance to make official something it never had before: “Korematsu was gravely wrong the day it was decided, has been overruled in the court of history, and — to be clear — has no place in law under the Constitution.”

Justices Stephen G. Breyer and Elena Kagan also dissented, in an opinion with less-explosive language.

Breyer said it was not clear that an important part of the order — its “elaborate system of exemptions and waivers” for refugees, asylum seekers, students and others — was working. “While more than 15,000 Syrian refugees arrived in the United States in 2016, only 13 have arrived since January 2018,” he wrote.

Breyer thought the case should be sent back to a lower court for an examination of that issue. But without that, he said, he would, “on balance, find the evidence of antireligious bias . . . a sufficient basis to set the Proclamation aside.”

Neal Katyal, the Washington lawyer who argued the case for Hawaii and other challengers, expressed disappointment at the ruling and called on Congress to step in.

“We continue to believe, as do four dissenting justices, that the travel ban is unconstitutional, unprecedented, unnecessary and un-American,” he said in a statement. “. . . The travel ban is atrocious policy, and makes us less safe and undermines our American ideals. Now that the Court has upheld it, it is up to Congress to do its job and reverse President Trump’s unilateral and unwise travel ban.”

The administration said the third edition of the ban responded to the judicial criticisms of the first two and was a result of a “worldwide review of the processes for vetting aliens seeking entry from abroad.”

But a unanimous ruling from a three-judge panel of the U.S. Court of Appeals for the 9th Circuit said that the third version of the travel ban suffered from the deficiencies of the first two — that Trump had again exceeded his lawful authority and that he had not made a legally sufficient finding that entry of those blocked would be “detrimental to the interests of the United States.”

The U.S. Court of Appeals for the 4th Circuit in Richmond struck down the ban on the constitutional question. The 9-to-4 decision took a deep dive into Trump’s statements and tweets, and concluded that the proclamation, like the first two, was motivated not by national security concerns but by antipathy toward Muslims.

Even the Supreme Court dissents did not make much of a claim that Trump lacked the statutory authority to bar the entry of those the administration had reason to believe could harm the country.

And Roberts rejected the notion that the court was prohibited from examining the reasoning for the president’s actions on immigration matters.

But Sotomayor said the majority failed by not finding that the policy was inspired by “animosity toward a disfavored group.”

“Our Constitution demands, and our country deserves, a Judiciary willing to hold the coordinate branches to account when they defy our most sacred legal commitments,” she wrote. “Because the Court’s decision today has failed in that respect, with profound regret, I dissent.”

Roberts seemed careful not to tie the court to Trump’s remarks.

“The President of the United States possesses an extraordinary power to speak to his fellow citizens and on their behalf,” he wrote, and was almost wistful in remembering presidents — he named George Washington, Dwight D. Eisenhower and George W. Bush — who “used that power to espouse the principles of religious freedom and tolerance.”

Justice Anthony M. Kennedy made a similar point in a concurring opinion.

“The very fact that an official may have broad discretion, discretion free from judicial scrutiny, makes it all the more imperative for him or her to adhere to the Constitution and to its meaning and its promise,” he wrote.

The case is Trump v. Hawaii.