Question: do constitutional rights apply to foreigners?
Answer: The constitution protects the rights of non-citizens within the US territory. You are technically outside US territory when you are still at the border or the airport. Thus, these constitutional rights don't apply. As a consequence, the government has full rights to deny entry.