The theory behind it is that it discourages people from doing those things again. If you're the guy that got everyone into a classroom detention over lunch, you're not going to be liked, and thus peer pressure will be on you to not do it again. Its less about being sure that you punish the wrongdoer, and more about getting people to stop wrongdoing. You also can't just let it pass. I had a teacher who did that. It just resulted in the same one or two kids being absolute fuck ups for their entire school career, because they never got punished. It actually got everyone to hate them, because they were just plain annoying by the final two years of primary school, and they never got punished - it wasn't fair - meanwhilst one of us would decide to twiddle our thumbs because we were bored in church and it was detention time.
Its a battle teachers can't really win though, and it needs a lot of thought in dishing out the punishment both for it, and for various other wrongdoings, if its to be at all effective.
As an example, the reason that it works in something like bootcamp is that you're stuck with everybody, and the punishments are HARSH. If it was half rations for the next 1 meal, nobody would really give too many shits. It'd become everyday, and its not really that stressing - until you start being under-nourished for training, but then you just have resentment for the instructor for not letting you eat enough to train. Yet this is the sort of punishment teachers give to students. A single lunchtime detention, then telling people off because they didn't do their homework during lunch, and nobody really cares that much about missing a lunchtime anyway - as the whole reason you're probably staying in is some people were fucking around in class, and god knows they're just going to fuck around in detention too.
If, in boot camp, everyone had segregated themselves into a set of cliques within each platoon, the punishment would be equally ineffective. The clique of the fuck ups would bond over the fucking up, and the other cliques wouldn't like them, but they wouldn't necessarily WANT to be liked by the other cliques, and could just deal with it with their own bonding. This is exactly what happens in schools. The clique that fucks up doesn't care about the other cliques, and tends to bond over the fuck-up and punishment experience, whilst everyone else resents it. This CAN foster discontent among other cliques who'll then dob them out, however often the clique doing the fuckupery is a more popular one, and since everyone in school wants to be popular [Except Tim, Tim just doesn't give a fuck], nobody wants to cross the popular kids.
Additionally, I'd hazard a guess that in general, drill instructor punishments for various fuck ups are more proportional than school teacher punishments. Sure, the drill instructor may start off harsher, but from that harsh point you know what to expect from your fuck ups. If you mess up in training, everyone might get pushups or something. If you start a fight, its going to be a lot worse than that, and if serious, you could end up getting discharged.
As said, at school, I got a detention for not paying attention in church. The school bully who beat up kids and stole their lunch money? Same punishment. Exact same. Never got any worse for him either. That didn't make the class resent him [Well, it did], but everyone resented the teacher too for not punishing him more.
If you had harsh punishments, managed to break down clique barriers, and dished out proportional punishments for various things, it could work. Sadly, this is rarely attempted, and thus teachers fail before they start when using the method. It is the best method at their disposal - seeing as they are incapable of planning out even that punishment so that it might work, they won't be able to execute a more elaborate punishment in all likelihood, and school restrictions may prevent them from doing so - but its also ineffective. TBH the entire education system needs a bit of an overhaul, but its pretty entrenched, and there are some who'll argue doing it this way has its benefits. We can only hope one day it'll improve, but I won't be holding my breath.