How does the writ of habeas corpus operate?

This is how the writ of habeas corpus operates to safeguard the liberty of a person:

The prisoner or any person on his behalf petitions the proper court, which immediately issues the writ. It is sent to the person having another in his custody. Such person is ordered to produce the prisoner in court at a specified time, together with an explanation of the cause of the detention, called the return. After the order is obeyed, the judge scrutinizes the return and then decides whether it shows that the imprisonment is authorized by law. If so, the prisoner is remanded (sent back to custody); otherwise, he is set free at once by the judge.