A judge has ordered the Trump administration to release a man that it wrongly detained and planned to deport to El Salvador.