Nature provided humans with an excellent learning mechanism to protect them from harm - pain.
You grab the stem of a thorny rose - it hurts and you stop doing that.
You put your hand on something hot - the pain makes you let go, and you learn not to do it again.
From an early age, our brains are wired to learn from pain and avoid the activities that bring pain.
Spanking is merely harnessing a natural process that we use to learn.
The problem with spanking, is that it is only useful in situations where the child can accurately connect the improper action with the punishment.
For 1-2 year old children, that means that spanking/swatting is only useful the instant they do something wrong. You have to direct their eyes to the problem, say no, and swat them immediately (just like a puppy).
For 3-5 year old children, you have to talk to them first, and ensure they understand what they did was wrong, and why - then follow it with a spanking.
After that, it's pretty much the same thing. Spanking can be a better alternative for some children than say...being grounded.
An excellent explanation (using puppies) can be found in Robert Heinlein's Starship Troopers (Ch. 8):
Teacher: "When your puppy made mistakes, were you angry?"
Student: "What? Why, he didn't know any better; he was just a puppy."
Teacher: "What did you do?"
Student: "Why, I scolded him and rubbed his nose in it, and paddled him."
Teacher: "Surely he could not understand your words."
Student: "But he could tell I was sore at him!"
Teacher: "But you just said you weren't angry."
Teacher: "But I had to make him *think* I was! He has to learn somehow."