Habeas Corpus is the primary means whereby an individual who is incarcerated can challenge the lawfullness of that detention. It has its origins in 14thC Britain but was formalised in 1679 with the Habeas Corpus Act. With the establishment of the Bill of Rights in the US Constitution a writ of Habeas Corpus become one of inalienable right.

The University of Chicago has the complete Habeas Corpus Act of 1679 online. The act's preamble explains why it is necessary:

Whereas great delays have been used by sheriffs, gaolers and other officers, to whose custody any of the King's subjects have been committed for criminal or supposed criminal matters, in making returns of writs of habeas corpus to them directed, by standing out an alias and pluries habeas corpus, and sometimes more, and by other shifts to avoid their yielding obedience to such writs, contrary to their duty and the known laws of the land, whereby many of the King's subjects have been and hereafter may be long detained in prison, in such cases where by law they are bailable, to their great charges and vexation:

Basically it is to stop indefinite and arbitrary detention from the executive. Habeas Corpus is a writ to appeal to the judicial against executive tyranny. It allows a prisoner to sue the executive for their freedom or have a charge leveraged against them.

II. For the prevention whereof, and the more speedy relief of all persons imprisoned for any such criminal or supposed criminal matters; ...

That whensoever any person or persons shall bring any habeas corpus directed unto any sheriff or sheriffs, gaoler, minister or other person whatsoever, for any person in his or her custody, and the said writ shall be served upon the said officer, or left at the gaol or prison with any of the under-officers, under-keepers or deputy of the said officers or keepers, that the said officer or officers, his or their under-officers, under-keepers or deputies, shall within three days after the service thereof as aforesaid

...

and shall then likewise certify the true causes of his detainer or imprisonment,

Badly chopped up unfortunately, but it is a rambling piece of legislation. Interesting to note that it demands action within three days of the writ being issued.

More reading: Tags, Habeas Corpus, 1679
Cam Riley: South Sea Republic. Freedom, liberty, equity and an Australian Republic.