A dog that follows someone to the bathroom is likely doing it because it's part of their instinct to stay near you.
Dogs that are following you to the bathroom are known as Velcro Dogs and the dog following you to the bathroom or anywhere else is a result of their animal instinct and pack mentality.
The dogs instinct is to follow you around even to the bathroom to help protect you as a part of their pack.
Not all dogs will follow you around or follow you to the bathroom but I've had a few dogs do that and then just sit there at the door until I came out of the bathroom.
As much as I loved my dog I hated being followed to the bathroom where I wanted privacy and always shut the door on the dog so that they can't sit there and watch me on the toilet. lol