Teach them to stroke animals gently from one side (never face on), from the top of the neck to the tail and never on the ears, which can be sensitive (especially for dogs).
Explain to your children that an animal isn't "nice" or "nasty": it's a living creature and if it senses danger it can run away or attack. Tell them never to take a pet's bowl away when it's eating, and keep your pet away from you when you're eating. Tell them not to look a dog in the eyes.
Involve your children in taking care of your pet if you can (walking, cleaning, going to the vet, cleaning out its cage or kennel, feeding, etc).
Drum it into them that they must never stroke an animal they don't know.