WASHINGTON - The US Supreme Court on Tuesday upheld President Donald Trump’s controversial ban on travelers from five mostly Muslim countries - a major victory for the Republican leader after a tortuous legal battle.

In a majority opinion written by Chief Justice John Roberts, the court ruled 5-4 that the most recent version of the ban, which the administration claims is justified by national security concerns, was valid.

“In short, the language... is clear, and the Proclamation does not exceed any textual limit on the President’s authority,” Roberts wrote.

“The Government has set forth a sufficient national security justification to survive rational basis review. We express no view on the soundness of the policy.”

The version of the travel ban at issue was the third iteration, and applies to travelers from North Korea and five mainly Muslim nations - Iran, Libya, Somalia, Syria and Yemen - or about 150 million people. Certain officials from Venezuela are also targeted by the measure.

“SUPREME COURT UPHOLDS TRUMP TRAVEL BAN. Wow!” a jubilant Trump tweeted after the ruling.

The decision - which follows a court hearing in April - is the culmination of a battle that began just days after Trump took office in January 2017.

One week into his presidency, he followed through with a campaign promise and announced a 90-day ban on travelers from Iran, Iraq, Libya, Somalia, Sudan, Syria and Yemen.

Prepared in secret, the sudden order created chaos as hundreds of travelers were blocked at airports.

Tens of thousands of legal visas were canceled and protesters took to the streets saying the president was banning Muslims in violation of the constitution’s religious freedom protections.

Courts in several states found the measure was illegal, and did so again in March 2017 after the Trump administration slightly amended the original order, with Iraq dropped from the list.

A furious Trump bashed the courts and his own Justice Department, but was forced to recast the ban again.

Issued in September, the latest version was open-ended, dropped Sudan, and added North Korea and a selection of Venezuelan officials.

Opponents - and the court’s liberal-leaning justices - decried what they saw as a measure targeting Muslim countries, and referred back to Trump’s record of anti-Muslim statements during the 2016 presidential election campaign. Justice Stephen Breyer, in a dissenting opinion, said he would have opted to “leave the injunction in effect” while the case was sent back to lower courts for further review.

He argued there was evidence that the government “is not applying the Proclamation as written,” citing examples of “anti-religious bias.”

Fellow dissenter Justice Sonia Sotomayor wrote: “Based on the evidence in the record, a reasonable observer would conclude that the Proclamation was motivated by anti-Muslim animus.”

The American Civil Liberties Union, which has been at the forefront of the fight against the ban, lamented the ruling.

“This ruling will go down in history as one of the Supreme Court’s great failures,” Omar Jadwat, director of the ACLU’s Immigrant Rights Project, said in a statement.

“We must make it crystal clear to our elected representatives: If you are not taking action to rescind and dismantle Trump’s Muslim ban, you are not upholding this country’s most basic principles of freedom and equality.”