The ruling prohibits federal officials from detaining or deporting immigrants and refugees with valid visas or green cards or forcing them to undergo extra security screenings based solely on President Trump’s order.