WASHINGTON (Reuters) - The U.S. Supreme Court on Tuesday handed Donald Trump one of the biggest victories of his presidency, upholding his travel ban targeting several Muslim-majority countries and rejecting the argument that it represented unconstitutional religious discrimination.

The 5-4 ruling, with the conservative justices in the majority and the liberal justices dissenting, ended a fierce fight in the courts over whether the policy amounted to an unlawful Muslim ban, while confirming broad presidential powers over immigration and national security policy.

Trump quickly claimed “profound vindication” after lower courts had blocked his travel ban announced in September, as well as two prior versions, in legal challenges brought by the state of Hawaii and others.

Trump has said the travel ban is needed to protect the United States against attacks by Islamist militants.

The ruling, denounced by civil rights groups and Democrats as well as protesters outside the courthouse, empowers Trump as he is embroiled in controversy over his approach toward illegal immigration along the U.S.-Mexican border.

Facing intense criticism, Trump retreated last week on his administration’s practice of separating the children of immigrants from their parents when families were detained illegally entering the United States.

The Supreme Court held that the challengers had failed to show that the travel ban violated either U.S. immigration law or the U.S. Constitution’s First Amendment prohibition on the government favoring one religion over another.

In remarks at the White House, Trump hailed “a tremendous victory for the American people and for our Constitution.”

“We have to be tough, and we have to be safe, and we have to be secure. At a minimum, we have to make sure that we vet people coming into the country,” the Republican president said, referring in a statement to “this era of worldwide terrorism and extremist movements bent on harming innocent civilians.”

The ban prohibits entry into the United States of most people from Iran, Libya, Somalia, Syria and Yemen. The Supreme Court allowed it to go largely into effect in December while the legal challenge continued.

Senator Bob Menendez, the top Democrat on the Senate Foreign Relations Committee, said: “Despite today’s ruling, turning away those fleeing horrific violence and persecution or to discriminate against people based on nationality and religion continues to be as un-American as ever.”

Writing for the court, Chief Justice John Roberts said that Trump’s administration “has set forth a sufficient national security justification” to prevail.

“We express no view on the soundness of the policy,” Roberts added.

The ruling affirmed broad presidential discretion over who is allowed to enter the United States. Trump could potentially add more countries to the ban.

Roberts said Trump’s actions suspending entry of certain classes of people were “well within executive authority and could have been taken by any other president - the only question is evaluating the actions of this particular president in promulgating an otherwise valid proclamation.”

The challengers had argued that the policy was motivated by Trump’s enmity toward Muslims and urged courts to take into account his inflammatory comments during the 2016 presidential campaign. Trump as a candidate called for “a total and complete shutdown of Muslims entering the United States.”

Hundreds of people gathered in New York City’s Foley Square on Tuesday evening to decry the Supreme Court decision. Many held banners reading: “No ban No wall No raids NY is for all.”

Slideshow ( 4 images )

“Just because something is deemed legal does not make it moral or just,” activist Linda Sarsour told the crowd, evoking earlier, since discredited Supreme Court rulings that affirmed slavery or Japanese internment camps.

‘STARK PARALLELS’

In a dissent she read in the courtroom, liberal Justice Sonia Sotomayor cited “stark parallels” with the court’s 1944 decision that upheld U.S. internment of Japanese-Americans during World War Two. Sotomayor also described various statements Trump made on the campaign trail.

“Taking all the evidence together, a reasonable observer would conclude that the proclamation was driven primarily by anti-Muslim animus,” Sotomayor added.

Slideshow ( 4 images )

In the ruling, Roberts officially repudiated the 1944 internment decision and rejected any comparison between the cases, saying that the war-era practice was “objectively unlawful and outside the scope of presidential authority.”

Roberts said it was “wholly inapt to liken that morally repugnant order to a facial neutral policy denying certain foreign nationals the privilege of admission.”

Chad initially was on the list of countries targeted by Trump that was announced in September, but he removed it on April 10. Iraq and Sudan were on earlier versions of the ban. Venezuela and North Korea also were targeted in the current policy. Those restrictions were not challenged in court.

“The ruling will go down in history as one of the Supreme Court’s great failures,” said Omar Jadwat, a lawyer for the American Civil Liberties Union, which challenged the ban.

The travel ban was one of Trump’s signature hardline immigration policies that have been a central part of his presidency and “America First” approach. Trump issued his first version just a week after taking office, although it was quickly halted by the courts.

Trump also has moved to rescind protections for young immigrants called “Dreamers” who were brought to the United States illegally as children, as well as acting against states and cities that protect illegal immigrants, ending protected status for certain immigrants in the country for decades, intensifying deportation efforts and pursuing limits on legal immigration.

The ruling means that most people seeking to enter the United States from the affected countries will need to navigate an opaque waiver process.

“If they are allowed to have this ban, what will they try next?” asked Mohamad Mashta, a Syrian who joined one of the lawsuits challenging the ban. Mashta is a permanent U.S. resident working as an engineer in Ohio whose wife, also Syrian, was able to obtain a visa after the ban was initially blocked.

With the policy in place, the number of people from the affected countries able to obtain visas has plummeted. [See graphic: tmsnrt.rs/2tyHpRa]