A hospital can hold you legally and force you to stay in the mental hospital against your will for 72 hours unless the hospital gets a court order to keep you in the mental hospital longer than 72 hours if you're a danger to yourself or other people.
After 72 hours and they haven't gotten a court order to force you to stay in the mental hospital longer than 72 hours then the mental hospital must release you from their custody and let you walk out the door.