In fact, it does pass legal muster.
I think it's US Code 8 (I forget the section number) that gives the President the blanket authority to prohibit entry of any class or type of aliens, if he feels it is in the national interest to do so.
The power of the President under that law is broad, sweeping, and absolute.
Doesn’t matter ... in the real world we live in now ... you won’t get the Supreme Court to go along with banning people into this country based on their religious faith. That would never fly which is why Trump changed it to specific countries. Heck — it is hard enough to ban immigrants from a country in this current climate much less adopt a religious test on immigrants.