In my school, we HAVE to enforce this. We have too many non-custodial parents showing up, aggressive people bursting in yelling at the teacher any time, (which terrifies the children) kids getting distracted by parents being in the room, (They get excited or upset if the parent is not their own) parents stopping lessons and demanding to speak to the teacher right now. That means the kids don't learn, since a parent didn't want to bother with an appointment or doesn't care about the needs of the other kids.
Granted, in my area, we have a few extreme parents that I would not say are the majority of people. But just like you wouldn't show up to a doctor or lawyer without an appointment, you have to know that the teacher is working with the kids, and the kids are working too, they cannot have their teacher taken away from them even for a moment because a parent didn't want to make an appointment. Should a doctor walk away from a patient for someone who just showed up? Should a meeting be interrupted spontaneously? If a teacher is working with a student on say, long division, then that child shouldn't have to wait while someone walks in. The kids need to come first. In my school, other teachers aren't allowed to just walk into classrooms, and intercom messages have to be kept to a bare minimum. Administrators look at schedules to plan when they come in, and observe silently, often walking funny so their feet don't make a noise. So it's not like we are targeting parents alone.
So, it's both safety, and consideration for all the kids. If it's really important, you can usually make an appointment the same day, you just have to make a two minute phone call. It'd be rude anywhere else to just show up, so be considerate of all the kids, staff, and other parents and make an appointment.