Have you ever wondered why, that when you need to go to the the toilet, you turn around to close the door and there they are, lovingly looking at you while you do your business?
The simple reason is that your dog is part of your family pack and with that, they feel they need to be with you at all times or they will feel a little insecure. This is a dog survival instinct as they have learned to do everything in a pack for maximum benefit; hunting and protecting territory is all successfully done in a group. To them you will never want to be alone if you are in their pack, and there is no exception to this rule even with the toilet or bathroom.
Dogs also get curious if you shut a door, of course they want to know what’s happening!