Children love household pets. Someone soft and friendly who never tells them "no." Someone with a lower status in the household than theirs. It's a real parenting challenge to teach our children to respect an animal who can't speak for itself, or tell the child when he or she is being too rough.
The first, and most important thing to do is to give your pet (let's say it's a boy and a dog-- Jimmy and Maggie) a safe place in the house to rest. Also, the dog's food and water dish should be off-limits to your son. When Maggie is on her bed in the living room, or in her kennel in the hall (for example), your child is simply not allowed to touch her. First, this lets your pet rest from Jimmy's enthusiastic attentions. It also lets Maggie know she is not doing anything WRONG by being in her space-- and that she does have a space of her own in the house, just like everyone else.
If you don't have a special dog bed or crate, go to goodwill and buy a special dog blanket. Create a corner for it in the room where the family spends most of their time. Not only will your dog be more likely to USE the blanket if it's near you, but you'll have an easier time keeping track of Jimmy's interactions with your dog while she's on the blanket. You're already in the room.
Explain to Jimmy that when Maggie is on her bed, we have to leave her alone. That is her special space. We don't put toys there, and we don't hug her or touch her when she's in her special space. This one act sets up an understanding with your son that your dog has rights, too. It'll take some reminders, and maybe some time-outs if Jimmy doesn't listen to you or remember the new rule.
The next step is to sit down with your son, and explain that animals feel hurt just like people do. We have to be more gentle with a baby because it is smaller than we are, and we have to be more gentle with Maggie because she isn't very big, either. Jimmy is still growing, and he's strong and healthy-- soon Jimmy will be bigger than Maggie (if he isn't already). So Jimmy needs to remember that he might hurt Maggie if he's too rough with her. Maggie loves him so much, she might not go away when he hugs too hard, or runs into her with his toy cars, but it still hurts her. Explain that you know Jimmy loves Maggie, too, and doesn't want to hurt her. This sets up your expectation-- and lets Jimmy be the good guy in the situation.
One controversial method of parenting, usually reserved for truly unacceptable behaviors like biting and hurting others with hard plastic toys, is to bite back. A child may simply not understand that what they do hurts the person or animal they are doing it to. We never want to do something like that in anger, and it may not be the right choice for your family or situation. We also never want to intentionally hurt a child. But I've occasionally seen the method used effectively to once--and only once-- and for all show a child why a behavior they enjoy is just plain wrong. It may be helpful to ask your child how he would like it if somebody did that behavior to HIM?! And then demonstrate how uncomfortable it can be.
Also, make sure that you show and tell your child how YOU pet and interact with Maggie the dog. Your child may not realize how different your treatment of the dog is from the things he is doing. Or that some things that were okay when he was little (like lying on top of the dog) are not okay now that he's gotten so big. The rules change as we grow up. Sometimes we have fewer rules, sometimes we get new ones. An adult can look like they are really rough-housing with a dog, but still use their adult skills for being gentle and in control of that interaction. It is important to rub a child's arm or back as hard as you do the dogs-- to explain and then SHOW your child just how hard you are really petting the dog. Or explain that it hurts to have your hair pulled-- it hurts Maggie just like it hurts Mommy or Jimmy.
Be aware that children imitate the behaviors they see adults exhibit. So if there is an adult in your household who hits, it will be a lot harder to explain to your child why hitting isn't okay. I remember my mom explaining to me that my dad was an adult, so he could decide whether or not to use swear words. But I was a child, and even though I heard dad using those words, it wasn't okay for me to use them until I became an adult, too. And she pointed out that she did NOT use those words. It was a choice that an adult could make, but that a 5-year-old could not. And that made sense. Mom decided if I could go play with friends, and many other things that I was not old enough to decide. Swear words (and maybe hitting) were just another thing on the list of decisions I didn't get to make. Then mom gave me other words that I COULD use to express those emotions.
My favorite angry word was "Fiddle-sticks!" Maybe there is a special pillow or stuffed animal in the house that it is okay for Jimmy to rough-house with and roll on and hit and throw, since it is NOT okay for him to do those things to the dog. When he's an adult like Mommy and Daddy, he'll get to decide how he treats his pets. But right now, Maggie is in her time-out space when she's on the dog blanket, and we all need to leave her alone when she goes there. Pets have the right to be treated with respect, and to have a safe place to sleep, just like we do.