US District Judge Jon Tigar in San Francisco issued the temporary restraining order after hearing arguments by civil rights groups.
Mr Trump signed the order earlier this month in response to the migrant caravan moving towards the border.
He cited national interest concerns but was opposed by civil rights groups.
They brought the San Francisco case saying his decision was illegal.
Judge Tigar, in his ruling, said current legislation makes it clear that any foreigner arriving in the US “whether or not at a designated port of arrival” can apply for asylum.
“Whatever the scope of the President’s authority, he may not rewrite the immigration laws to impose a condition that Congress has expressly forbidden,” he added.