Immigrants facing deportation can be held by the federal government for months or even years without the chance to go to court and seek bail, the court ruled.