It's not too hard for a doctor to keep you in a psych unit against your will for 72 hours. If the doctor wants to hold you longer - against your will - it gets more complicated. Bear in mind that psychiatric beds are in very short supply. There's a lot of competition for those beds. It's actually kind of hard to even get a bed in a psych facility . . . for a short stay, nevermind a prolonged stay.
But - back to the issue of involuntary commitment. Putting a psych patient on a 72 hour hold is pretty routine. A doctor can do that. In some states, a doctor can even petition the court to authorize a 15 day hold. That doesn't always require a hearing. Where I worked, I never knew of anyone being kept more than 72 hours (involuntarily) without a hearing being held, where a judge came in to hear the testimony of the psychiatrists, therapists and the patient. I used to sometimes hear doctors saying they thought a patient needed to be kept longer, but they didn't think the judge would approve their petition for commitment.
A patient under involuntary commitment has a right to a hearing being held and an opportunity to ask the judge to approve an order to be released. I'm not sure how speedily that has to be arranged, but, it's somewhere between 72 hours and two weeks. The patient has a right to be there and talk to the judge. You even have a right to an attorney. If you're poor, an attorney will be provided to you for free. Here's a manual about how it works in one state:
OPA: Your Rights in a Psychiatric Facility
I have known of doctors ordering a patient to be transported to a state psychiatric facility. But, even there, the patient still has a right to a hearing before a judge within a reasonable amount of time. Like I said, every hospital has a conference room where judges hold these hearings. Typically, the patient is escorted to the hearing room by one or two mental health techs, depending on the patient's behavior and propensity toward violence. The judge is obligated to explain your rights to you.
A main point I want to make is that doctors are not lawyers. Don't ever depend on a healthcare professional to give you advice about your legal rights. If you do, you're apt to get a bunch of misinformation.
I have known hospital personnel to tell patients things that were just flat out untrue. In-patients have a right to call Adult Protective Services. Also, they have a right to contact an ombudsman, who can help resolve a conflict between a patient and hospital staff.
There is another thing to bear in mind: You can't have your cake and eat it too. When you present yourself at a psych unit, saying that you have very serious plans to harm yourself, then you have a duty to cooperate with the staff in formulating a reasonable plan to keep yourself safe. Otherwise, you do have the option of going off into the woods and quietly hanging yourself . . . . . and no one will bother you.