The immigration agency must hold fewer detainees and provide them with medical care and regular access to lawyers, Judge Lewis A. Kaplan said.