Khalil has been deprived of his freedom since March 8 without being charged with a crime. President Donald Trump's government wants to deport him for his participation in pro-Palestinian demonstrations on campus.
The judge has given the government until Friday to appeal and set a bail for Khalil of one dollar.
Foreign Minister Marco Rubio has said that Khalil's stay in the country risks having "serious consequences" for the US's foreign policy.
But the same court has already previously established that the reason is likely to be in conflict with the constitution. Now the judge says that Khalil has shown that the time in custody causes irreparable harm to his career, family and freedom of speech.
During last year's pro-Palestinian demonstrations at the prestigious Columbia University, Khalil acted as a mediator between the activists and the university's management.
The White House claims that propaganda for the terrorist-stamped Hamas was distributed at protests arranged by Khalil.