A deeply divided Supreme Court upheld President Trump's immigration travel ban against predominantly Muslim countries Tuesday as a legitimate exercise of executive branch authority.

The 5-4 ruling reverses a series of lower court decisions that had struck down the ban as Illegal or unconstitutional. It hands a major victory to Trump, who initiated the battle to ban travelers a week after assuming office last year. It was a defeat for Hawaii and other states that had challenged the action, as well as immigration rights groups.

Trump called the ruling a "tremendous victory" after first tweeting seven simple words: "SUPREME COURT UPHOLDS TRUMP TRAVEL BAN. Wow!"

"In this era of worldwide terrorism and extremist movements bent on harming innocent civilians, we must properly vet those coming into our country," he said in a statement. "This ruling is also a moment of profound 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."

The president had vowed to ban Muslims during the 2016 presidential campaign and continued his attacks on Twitter after his election. But the high court said those statements did not constitute evidence of religious discrimination.

Chief Justice John Roberts issued the opinion, supported by the court's other four conservatives -- a majority that has won a dozen 5-4 cases this term. He said entry restrictions were limited to countries previously designated by Congress or prior administrations as posing national security risks. And he noted that it followed a worldwide review process by several government agencies.

"The proclamation is squarely within the scope of presidential authority," the chief justice said. He added that claims of religious bias against Muslims did not hold up against "a sufficient national security justification." However, he said, "We express no view on the soundness of the policy."

The court's four liberal justices dissented, with Justices Stephen Breyer and Sonia Sotomayor reading from the dissents in court, a rare occurrence. Breyer found "evidence of anti-religious bias" and was joined by Justice Elena Kagan.

In a separate dissent in which she extensively quoted Trump's words during and after the 2016 campaign, Sotomayor said: "A reasonable observer would conclude that the proclamation was motivated by anti-Muslim animus." She was joined by Justice Ruth Bader Ginsburg.

"What began as a policy explicitly 'calling for a total and complete shutdown of Muslims entering the United States' has since morphed into a “proclamation” putatively based on national-security concerns," Sotomayor said. "But this new window dressing cannot conceal an unassailable fact: the words of the president and his advisers create the strong perception that the proclamation is contaminated by impermissible discriminatory animus against Islam and its followers."

Seventeen-month journey

The legal battle began immediately after Trump issued his first travel ban in January 2017. That 90-day ban on travelers from Iran, Iraq, Libya, Somalia, Sudan, Syria and Yemen, and a 120-day ban on refugees worldwide, was struck down by federal district and appeals courts the following month.

Trump's second version, issued in March 2017, dropped Iraq from the list of affected nations and exempted visa- and green card-holders. It fared no better, getting struck down last spring before the Supreme Court ruled a year ago that travelers without close ties to the USA could be barred while vetting procedures were reviewed.

After Trump issued his third version in September — subtracting Sudan, adding Chad, North Korea and government officials of Venezuela, setting separate criteria for each country and making it indefinite rather than temporary — federal courts again struck it down. In December, the justices allowed it to go into effect, and in January, they scheduled it for oral argument.

Hanging in the balance were nearly 150 million residents of Iran, Libya, Somalia, Syria and Yemen. Chad, also majority-Muslim, was removed from the list in April. North Korea and Venezuela are not part of the legal battle.

Neal Katyal, the former U.S. acting solicitor general who argued the case against the travel ban, said the demise of the first two versions in lower courts "forced a recalcitrant administration to at least give its order the veil of constitutionality."

"The final chapter has not yet been written, and the president would be mistaken to interpret today’s decision as a green light to continue his unwise and un-American policies," Katyal said. "As the Supreme Court has repeatedly said, not everything that is constitutional is good policy. 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."

Liberal courts overruled

The case reached the Supreme Court from two liberal federal appeals courts — the 9th, based in San Francisco, and the 4th, based in Richmond, Va. Those courts and the district judges below said courts can and should examine the purpose behind government actions; that Trump's words revealed his purpose to be, at least in part, banning Muslims; that his initial focus on six majority-Muslim nations was a means to that end; and that Trump as president cannot distance himself from Trump as candidate.

Indeed, most of the judges who issued rulings on Trump's travel ban said his statements as a candidate, president-elect and president were relevant.

"These statements, taken together, provide direct, specific evidence of what motivated both (executive orders): President Trump’s desire to exclude Muslims from the United States," Chief Judge Roger Gregory wrote for the U.S. Court of Appeals for the 4th Circuit in a 10-3 ruling.

Katyal contended in legal papers that "for over a year, the president campaigned on the pledge, never retracted, that he would ban Muslims from entering the United States."

On the other side, some judges and legal analysts argued that campaign promises should be off-limits, or at least dwarfed by government actions that are not overtly discriminatory.

Judges and legal analysts who defended the travel ban argued that Trump's words cannot form the basis for a constitutional violation. It takes too much interpretation, they said, to read anti-Muslim bias into an executive order or proclamation that, on its surface, is devoid of religious content.

Several judges went further, arguing that campaign promises should be off-limits, or at least dwarfed by government actions that are not overtly discriminatory.

"Opening the door to the use of campaign statements to inform the text of later executive orders has no rational limit," 4th Circuit Judge Paul Niemeyer wrote in a dissent. He mused that such past history could extend to "statements from a previous campaign, or from a previous business conference, or from college."