I believe, in essence, it has to do with the cats both being curious and wanting to keep us companionship. With that being said, I think an additional and significant factor attracting cats to follow people to the bathroom could also be their common fascination with running water - cats are known to frequently have an obsession with sinks, faucets, drains and porcelain thrones for that reason.

It has to do with their instincts - cats prefer running water over stagnant water, because in nature it is a lot safer to drink; they are thus intrigued and interested by both the sight and the sound of running water. Because the majority of visible and audible water flow occurs whenever there is a person inside the bathroom, they are compelled to follow in.