The decision could potentially allow thousands of immigrants who have been subject to mandatory detention to be released.