In most cases you can sign out AMA. If you are a danger to yourself or others, you may be held against your will for a limited amount of time.
By the way, laws are very different in different places, so it helps to specify the country and state or province if you want to know what laws apply where you are right now.
You are asked to sign a release and state that you are leaving the hospital, "against medical advice." If you refuse to sign your on your own anyway, the hospital simply documents the details of your leaving AMA and that's that.. Your only hurting yourself it's not anyone else's responsibility at that point.