The question is: what way do most people go in case of an emergency and how can we make that way the easiest?
If the door of a bathroom would open to the outside, the door would block the path (the hallway) people would take in case of an emergency. It would block more people than it would help escape the bathroom. The same with the doors of offices, etc. No one needs to enter a bathroom in case of an emergency (well, a different kind of emergency...), so it doesn't matter if it may blocked to the inside. So doors to closed rooms adjacent to a hallway open to the inside, while doors in the emergency path of the entire stream of people open to the outside.
Of course, there are situations where emergency paths cross and neither option is ideal. However, for most cases, the rule is pretty clear and obvious.
If the door of a bathroom would open to the outside, the door would block the path (the hallway) people would take in case of an emergency. It would block more people than it would help escape the bathroom. The same with the doors of offices, etc. No one needs to enter a bathroom in case of an emergency (well, a different kind of emergency...), so it doesn't matter if it may blocked to the inside. So doors to closed rooms adjacent to a hallway open to the inside, while doors in the emergency path of the entire stream of people open to the outside.
Of course, there are situations where emergency paths cross and neither option is ideal. However, for most cases, the rule is pretty clear and obvious.