WASHINGTON: The Supreme Court on Tuesday handed President Donald Trump the most significant legal victory of his presidency, upholding the administration’s ban on foreign visitors and immigrants from several mostly Muslim countries.

By a 5-4 vote, the court’s conservative justices bolstered the chief executive’s power to control the borders, just as he is battling a growing crisis over the separation of families crossing illegally along the Mexico border.

The majority rejected arguments that Trump overstepped his presidential authority and that his targeting of Muslim-majority countries violated the Constitution’s ban on religious discrimination.

“For more than a century, this court has recognized the admission and exclusion of foreign nationals” is a matter for the president and Congress, and is “largely immune from judicial control,” Chief Justice John G. Roberts Jr. said for the court. “Foreign nationals seeking admission have no constitutional right to entry.”

The majority dismissed claims that Trump’s history of negative comments about Muslims — including a call during the 2016 presidential campaign for a Muslim ban — were relevant to the validity of his final travel order.

The current ban covers five Muslim-majority nations — Iran, Libya, Somalia, Syria and Yemen — as well as North Korea and some government officials from Venezuela. The administration was forced to revise the original order twice to resolve legal problems over due process, implementation and its exclusive targeting of Muslim nations.

The ruling is a major victory for Trump and his administration as well as an implicit rebuke to the judges on the East and West coasts who repeatedly issued nationwide orders to block the travel ban.

Roberts pointed to one broadly worded provision in an immigration law that says the president may “suspend the entry … of any class of aliens” if he believes they “would be detrimental to the interests of the United States.” After a “multi-agency review … the president lawfully exercised that discretion,” he said in Trump vs. Hawaii.

Justices Anthony M. Kennedy, Clarence Thomas, Samuel A. Alito Jr. and Neil M. Gorsuch agreed.

The court’s decision comes as the Trump administration has attempted to impose a “zero-tolerance” policy against foreigners who illegally cross the southern border. That sparked an international backlash after more than 2,000 children were forcibly separated from their parents, who are being held in immigration jails.

While Tuesday’s ruling does not deal with the arrest or prosecution of border crossers, it will likely be cited by Trump’s lawyers as strengthening his authority along the borders.

The four liberal dissenters said Trump’s order reflected bias against Muslims. Justices Sonia Sotomayor and Stephen G. Breyer read lengthy dissents in the courtroom Tuesday to express their displeasure.

Sotomayor cited Trump’s campaign pledge to enact “a total and complete shutdown of Muslims entering the United States.”

Trump praised the ruling as a “tremendous victory for the American people and the Constitution. 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. “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.”

Muslim individuals and groups, as well as other religious and civil rights organizations, expressed outrage and disappointment at the high court’s decision. Protests were being planned or staged across the country.

In a statement emailed to the Associated Press, American Civil Liberties Union attorney Lee Gelernt said it’s clear “that the president for political reasons chose to enact a Muslim ban despite national security experts, both Democrat and Republican,” who counseled against it. Heidi Beirich of the Southern Poverty Law Center said “immigration policy should never be decided based on race or religion.”

The controversy began during Trump’s first week in office. The first travel ban was announced with little notice, sparking chaos at airports and protests across the nation.

While the ban has changed shape since then, it has remained a key part of Trump’s vision.

A nonprofit group that supports Trump’s policies called the ruling ‘‘monumental.’’ Erin Montgomery, a spokeswoman for America First Policies, said, ‘‘[The decision] states that deciding who can and cannot enter our country does indeed fall within the realm of executive responsibility.’’

The travel ban has been fully in place since December, when the justices put the brakes on lower court decisions that had blocked part of it from being enforced.

The Associated Press contributed to this report.