The US supreme court has upheld Donald Trump’s travel ban targeting several Muslim-majority countries, in a significant victory for the administration and a blow to anti-discrimination advocates.

In a 5-4 ruling handed down on Tuesday, the court accepted the government’s argument that the ban was within the president’s power to craft national security policy and his authority to “suspend entry of aliens into the United States”.

Trump hailed the ruling, calling it a “tremendous victory for the American people”.

In a statement, the White House said the supreme court had “upheld the clear authority of the president to defend the national security of the United States” and called the ruling “a moment of profound vindication following months of hysterical commentary from the media and Democratic politicians”.

Opponents of the ban have said it has not made the country safer, while singling out Muslims for unfair treatment and violating constitutional protections against discrimination on religious grounds.

“This hateful policy is a catastrophe all around – not only for those who simply want to travel, work, or study here in the States, but for those seeking safety from violence as well,” Ryan Mace of Amnesty International USA said in a statement.

“The Muslim ban’s bigotry should have been as clear to the supreme court as it is to the Muslims demonized by it,” a statement from the Council on American-Islamic Relations (Cair) said. “Apparently, everyone but the supreme court can see the decision for what it is: an expression of animosity.”

Even as the court upheld the ban, Chief Justice John Roberts criticised Trump, and emphasised the importance of the principle of religious non-discrimination in US history.

This ruling will go down in history as one of the supreme court’s great failures Omar Jadwat, ACLU

“The president of the United States possesses an extraordinary power to speak to his fellow citizens and on their behalf,” Roberts wrote. “Our presidents have frequently used that power to espouse the principles of religious freedom and tolerance on which this nation was founded.”

Roberts pointed out that George W Bush defended “the true faith of Islam” after the September 11 attacks and said America is “a great country because we share the same values of respect and dignity and human worth”.

“Yet it cannot be denied,” Roberts wrote, “that the federal government and the presidents who have carried its laws into effect have – from the nation’s earliest days – performed unevenly in living up to those inspiring words.”

Trump has issued three executive orders curbing travel from certain Muslim-majority countries. Lower courts have blocked various versions of the ban. The current order was allowed to come into full effect in December 2017.

The ban targets travelers from Syria, Iran, Libya, Yemen and Somalia. It also includes limited sanctions against North Korea and Venezuela.

Neal Katyal, the primary lawyer who argued against the administration’s travel ban, condemned the supreme court’s decision as “unconstitutional, unprecedented, unnecessary and un-American”.

“We decided long ago that America doesn’t exclude people based on nationality or religion alone. Today, that principle has been challenged,” Katyal said in a statement.

Katyal, who served as the deputy solicitor general in the Obama administration, nonetheless sought to underscore the progress that had been made in watering down the travel ban from the policy Trump first unveiled in January 2017. After several successful challenges in court, Katyal said, the Trump administration had been forced to bring the travel ban “more in line” with the constitution.

“While we continue to believe that this third version fails that test, there is no question that by striking down the first two travel bans, the judiciary forced a recalcitrant administration to at least give its order the veil of constitutionality.”

Roberts’s opinion did not address the question of whether it was intended to exclude Muslims specifically.

In a dissenting opinion, Justice Sonia Sotomayor found “stark parallels” between Tuesday’s decision and Korematsu v United States, the 1944 ruling in which the supreme court upheld the internment of Japanese Americans.

“As here, the government invoked an ill-defined national security threat to justify an exclusionary policy of sweeping proportion,” Sotomayor wrote. “As here, the exclusion was rooted in dangerous stereotypes about, inter alia, a particular group’s supposed inability to assimilate and desire to harm the United States.”

The American Civil Liberties Union also accused the court of repeating the Korematsu decision.

“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.

Roberts rejected the link to Korematsu, however.

The president issued his first travel ban just a week after taking office in January 2017, prompting widespread chaos and protests at airports and in cities across the US.

The appeals process ultimately forced the conservative-leaning supreme court to determine the validity of the travel ban, which in its third version seeks to bar or limit entry to immigrants from the five Muslim-majority countries.

Opponents of the travel ban argued the policy was a watered-down attempt by Trump to make good on his campaign pledge to ban all Muslims from coming to the US.

The first Muslim elected to Congress, Minnesota congressman Keith Ellison, a Democrat, said the ruling was unjust and “will someday serve as a marker of shame” for the US.

While earlier versions of Trump’s travel ban indefinitely suspended all refugee admissions to the US, the president signed an executive order in October that resumed the processing of refugees. In doing so, Trump also called for a 90-day review of the program for 11 countries, most of them Muslim-majority, which are deemed “high risk” by his administration.