They decided on the outcome, then worked backwards to find a justification. Permit me to demoralize these judges by saying that ignoring terrorist presence in these countries destroys their credibility and authority. You really need blinders to come to this conclusion. I would call it a willful suspension of reality.
The facts of the case are that Trump banned entry for citizens of 7 countries. The countries are Iraq, Iran, Syria, Libya, Somalia, Sudan and Yemen. The countries are not named in the executive order itself. Instead they are incorporated in the order by reference to a law, "
The law, the Immigration Act of 1924, as amended in 1952, says, "Whenever the President finds that the entry of any aliens or of any class of aliens into the United States would be detrimental to the interests of the United States, he may by proclamation, and for such period as he shall deem necessary, suspend the entry of all aliens or any class of aliens as immigrants or nonimmigrants, or impose on the entry of aliens any restrictions he may deem to be appropriate."
As far as religion being a qualification for entry, it has been used that way in the past. It's part of having a well founded fear of persecution. I know a lot of former Soviet Jews who were granted visas to the US because of their religion, which made it hard for them to live in the Soviet Union. Nobody thought that was unlawful discrimination. The Lautenberg Amendment, enacted in November 1989, lowered the burden of proof of persecution for Soviet Jews, Evangelical Christians, and members of the Ukrainian Catholic and Ukrainian Autocephalous Orthodox Church to obtain refugee status to the United States.6 These groups would have "strong likelihood of qualifying for admission to the United States as refugees because their groups have a history of persecution." It required immigration officers to consider whether "historical circumstances" might give refugees a "credible basis for concern," rather than the "well-founded fear" they had been required to prove. Some believed the amendment made every Soviet Jewish emigre a potential refugee.
The 9th Circuit Court of Appeals has found that foreigners, who have never ever been to the US, have a right to enter, which the President of the US can't take away even though he is authorized by law to do so when he believes they may be a threat to national security.
Perhaps any federal judge in a lower court who has been reversed on three cases (3 strikes) in the final case outcome, whether in the Appellate Courts or the Supreme Court, should be subject to removal by a majority of both Houses of Congress. This would take a Convention of the States, but it would be a way to check judges who repeatedly ruled on what they would like the law to be, rather than what the law is. I think if the rule were in place today, the 9th Circuit would have very different judges.