A hospital cannot hold you against your will unless it's a mental hospital.
The only hospital that can hold you against your will is a mental health hospital or a hospital that has a mental health psych ward inside it.
And even then you have to be admitted to the mental health portion or psych ward of the hospital before they can legally hold you against your will.
The hospital can pink slip you which means they can legally hold you against your will when they see you as a threat to the public, yourself etc.
And then if you somehow walk out of the mental hospital then the staff will contact the police to find you and legally bring you back.
If in a mental hospital you have to be discharged before you can legally leave the mental hospital.
The mental hospital can legally hold you against your will for up to 72 hours and unless they get a court order to hold you any longer then the mental hospital must legally let you go.
However like mentioned above if you're just in a regular hospital for an illness or health issue etc then you can legally walk out of the hospital at anytime.
However if it's a child and no parent is around then they can legally hold the child against their will until the parents get there and they discharge the child or the parent walks out with the child.
The parent can legally take the child out of the hospital and take them home or to another hospital if they want too.