Ubasses, it is illegal to lock anyone into a home, dementia or not. Unless, as bednobs says, there Is a deprivation of liberty order. And those are given out very rarely, for exceptional circumstances and are usually very temporary.
Everyone is free to come and go as they like, unless they have either been arrested or Sectioned (under certain sections). The care staff will usually try to distract someone from leaving or at least accompany them, but they cannot be stopped an there aren't always enough staff to send, so the police have to be informed.