A hospital can only hold you or keep you against your will if you're a mental patient in the prison ward or the psychiatric ward.
If you're just a patient there for your health then you cannot legally be held against your will in the hospital and can leave freely at anytime you want too.
No it's not illegal to leave the hospital without being discharged.
As long as you're not in the mental health part of the hospital where they are legally holding you there and have the doors locked then you're free to walk out of the hospital at anytime.
If you do want to leave the hospital without the doctor discharging you then you need to sign a paper relieving the hospital of any liability for your health or should you die or get really sick.
But after you sign that paper you're free to leave the hospital at anytime you want too.
The hospital is not a jail or prison and cannot hold you against your will unless you're a mental patient that has legally held you there.
I've walked out of hospitals before and all I needed to do is get my stuff and sign a paper relieving the hospital of any liability.
Then I walked right out the door.