Demonstrators outside the Supreme Court on Wednesday. (Al Drago/Bloomberg News)

IF A national security policy is irrational and inspired by religious bias, can it still be legal? The Supreme Court had to weigh this question Wednesday as it considered a challenge to President Trump’s travel ban. Morally and practically, there is no question the ban is a bad idea. Legally, the Trump administration may yet prevail.

The travel ban in question is the latest in a series of patched-together executive orders from the White House, each one seeking to give the president’s actions more legal cover as courts have expressed skepticism. It restricts the entry of nationals from eight countries, most of them predominantly Muslim, and it does so on the pretext that these nations’ governments do not provide minimum amounts of information necessary to vet travelers.

Though Congress designed an immigration system requiring strong checks on travelers, the government argues that lawmakers also gave the president vast authorities to ignore this system and create his own, pointing to statutory language that allows the executive to turn away “any aliens” on national security grounds. Further, because the ban is construed as a national security policy, the Trump administration argues that the courts should show deference to the president’s judgment, as he is privy to information that judges are not, and allow the ban’s implementation as long as the government provides some national security rationale.

The state of Hawaii and other challengers respond that Congress did not give the president unlimited authority over immigration, as the government’s argument suggests. The language empowering the president stemmed from wartime authorities understood to allow the president to restrict the entry of spies and subversives, not entire nationalities. Before Mr. Trump, presidents used it in a limited fashion. Congress also insisted that visas could not be denied on account of nationality. If the president can simply deny entry to anyone he wants, why would lawmakers have included this limitation?

Even if that statutory argument doesn’t carry the day, the travel ban faces a steep constitutional challenge. Statement after statement — mostly before the campaign but also after — suggests that Mr. Trump sought to implement a Muslim ban as far as possible in the guise of this travel ban. In the real world, a person’s nationality has little to do with the likelihood of their posing a danger, as a Department of Homeland Security assessment and a range of former intelligence officials confirmed. The common-sense conclusion from this record is that the travel ban is inspired by anti-Muslim animus and irrational on national security grounds.

The court is properly wary of substituting its national security judgments for those of the president, and campaign rhetoric is not always indicative of underlying motives. But sometimes what is obvious is also true.