Nursing homes are not jails or prisons or mental health facilities and they have no legal rights to keep a patient against their will.
If you're of sound mind and can make decisions yourself easily and want to leave the nursing home you can do so whenever you like.
The nursing home would want you to sign a release form that would allow them to release you from the nursing home and to relieve them of any bad things that may happen to you if you do leave the nursing home.
Now if a patient has dementia really bad they may try to force the patient to stay in the nursing home and not leave because they could hurt themselves.
But if someone comes by and signs the dementia patient out of the nursing home then they are free to leave.