WASHINGTON — The Supreme Court cleared the way on Monday for President Trump to prohibit the entry of some people into the United States from countries he deems dangerous, but the justices imposed strict limits on Mr. Trump’s travel ban while they examine the scope of presidential power over the border.

Mr. Trump quickly hailed the court’s decision to hear arguments on the travel ban in October, saying — in a formal White House statement, not a tweet — that the justices’ temporary lifting of some of the legal roadblocks to his ban was a “clear victory” for national security.

“As president, I cannot allow people into our country who want to do us harm,” Mr. Trump wrote, calling his efforts to limit entry into the country a “suspension” instead of a ban. “I want people who can love the United States and all of its citizens, and who will be hardworking and productive.”

The Run-Up

The podcast that makes sense of the most delirious stretch of the 2016 campaign.

He later tweeted: “Very grateful for the 9-O decision from the U. S. Supreme Court. We must keep America SAFE!”

But those challenging the travel ban said the court’s opinion would protect the vast majority of people seeking to enter the United States to visit a relative, accept a job, attend a university or deliver a speech. The court said the ban could not be imposed on anyone who had “a credible claim of a bona fide relationship with a person or entity in the United States.”

Karen Tumlin, legal director of the National Immigration Law Center, said advocates for refugees and other immigrants would urge the justices this fall to lift the president’s travel ban for everyone seeking to come to the United States.

“We think it’s repugnant to our values that they might be treated differently because of where they are from or how they choose to pray,” Ms. Tumlin told reporters.

The court’s opinion sets up a historic legal clash in which the justices will weigh the president’s power to set national security priorities against the need to protect individuals from discrimination based on their religious beliefs or national origin.

In saying they would take the case, the justices partly endorsed the administration’s view that the president has vast authority to control who crosses the border. They said the president’s powers to limit immigration “are undoubtedly at their peak when there is no tie between the foreign national and the United States.”

But the opinion also signaled that some of the justices might believe that Mr. Trump exceeded even that broad authority when he twice sought to impose a blanket ban on entry to the United States from certain predominantly Muslim countries. With the limits imposed on Monday by the court, the travel ban will be far narrower than the one he proposed in his first week in office and a later, revised version.

For Mr. Trump, the opinion was a rare legal victory after months in which the lower courts repeatedly chastised him for imposing a de facto ban on Muslims’ entering the country. In May, the United States Court of Appeals for the Fourth Circuit, in Richmond, Va., said the president’s revised order “drips with religious intolerance, animus and discrimination.”

In a statement, officials at the Department of Homeland Security said the court opinion would allow the department “to largely implement the President’s executive order.” Mr. Trump used similar language in his statement, saying his travel ban would now “become largely effective.”

Critics of the ban disputed those assessments. Cecillia Wang, the deputy legal director for the American Civil Liberties Union, said the opinion meant that the ban would not apply to many people while the court case proceeds.

“Clearly, the White House press statement today is based on alternative facts,” Ms. Wang said.

The court’s decision could lead to months of administrative and legal wrangling as consular officials try to determine which people are allowed to seek entry into the United States and which are barred by the opinion.

“We are going to be monitoring all of that,” said Becca Heller, the director of the International Refugee Assistance Project, one of the plaintiffs in the case.

The justices said the distinction should be easy to administer. “In practical terms, this means that” the executive order “may not be enforced against foreign nationals who have a credible claim of a bona fide relationship with a person or entity in the United States,” they wrote.

But Justice Clarence Thomas, who issued a partial dissent on Monday that was joined by Justices Samuel A. Alito Jr. and Neil M. Gorsuch, warned that the court’s opinion would “prove unworkable” for officials at consulates around the world and would invite “a flood of litigation” from people denied entry.

“Today’s compromise will burden executive officials with the task of deciding — on peril of contempt — whether individuals from the six affected nations who wish to enter the United States have a sufficient connection to a person or entity in this country,” Justice Thomas wrote.

Based on the dissent, those three justices are likely to vote in favor of the Trump administration.

The court’s four-member liberal bloc — Justices Ruth Bader Ginsburg, Stephen G. Breyer, Sonia Sotomayor and Elena Kagan — are likely to vote against it.

That leaves the ultimate fate of the ban in the hands of Chief Justice John G. Roberts Jr. and Justice Anthony M. Kennedy.

Attorney General Jeff Sessions called the court’s decision an “important step towards restoring the separation of powers between the branches of the federal government,” and he expressed confidence that the court would uphold the president’s travel ban in its entirety after it heard the case this fall.

Some opponents of the travel ban said they worried about the fate of people who might be barred from entering the United States in the meantime.

“The court’s ruling will leave refugees stranded in difficult and dangerous situations abroad,” said Hardy Vieux, the legal director of Human Rights First. “Many of these individuals may not have ‘bona fide relationships,’ but have strong reasons to look to the United States for protection.”

Mr. Trump’s revised executive order, issued in March, limited travel from six mostly Muslim countries for 90 days and suspended the nation’s refugee program for 120 days. The time was needed, the order said, to address gaps in the government’s screening and vetting procedures.

Two federal appeals courts had blocked critical parts of the order, and the administration had asked that the lower court rulings be stayed while the case moved forward. The Supreme Court granted part of that request in its unsigned opinion.

The Fourth Circuit ruled on constitutional grounds, saying the limits on travel from the six countries violated the First Amendment’s ban on government establishment of religion. It blocked those limits, but not the suspension of the refugee program.

This month, the United States Court of Appeals for the Ninth Circuit, in San Francisco, blocked both the limits on travel and the suspension of the refugee program. It ruled on statutory rather than constitutional grounds, saying Mr. Trump had exceeded the authority granted him by Congress.

The Supreme Court agreed to review both cases in October, noting that the government had not asked it to act faster.

The court suggested that the administration could complete its internal reviews over the summer, raising the prospect that the case could be moot by the time it is argued.