This really should be a no-brainer: you can't break the law to enforce the law. When you try, you end up fighting yourself.
So it's no surprise that the courts are siding with the prisoners in these cases, since our military and intelligence community have disregarded the law in detaining and interrogating them.
For those (like me) who slept through high school civics, a writ of Habeas Corpus is essentially legal proof that an authority (e.g., the police) has the right to detain someone. The idea is that anyone can demand such a writ, and this places the burden of proof on the detaining authority rather than on the prisoner. It's similar to the "innocent until proven guilty" principle.