There are many ways to pet a dog. If you are doing it right, then you'll instantly become a dog's favorite person. But if you make him nervous, you'll be the number one person the dog wants to avoid - ouch!
The golden rule in petting is not to attempt to pet a dog that doesn't initiate contact. Children get excited about dogs, often running towards them and cornering them into an area, leaving the dog stressed out or anxious.
Rather than instantly touching the dog, start squatting down so that you both are on the same level, and avoid straight eye contact on your first meeting (eye contact can be seen as a threat too). Make yourself less threatening by turning your body slightly to the side and slowly let the dog sniff your hand. Avoid jumping or hovering over a dog when petting him because the dog can interpret you as a dangerous person.
You will know that a dog is eager to make friends when his ears are slightly held back, and his tail is wagging widely, on middle height. Most dogs also sniff your body, but this does not mean he wants to be petted instantly - most of the time, he wants to know you better. When a dog jumps or moves away, don't pet him. Discern if the dog is relaxed by looking at his eyes and mouth. If his facial expression looks happy and he moves towards you to initiate eye contact, he wants to be friendly, and this is the time to pet him.
There are a few best spots to pet a dog: on his chest, shoulders, or the base of his neck where the collar hits. Remember to start handling the dog on the side, and don't aim for the top of his head right away. Most dogs don't want to be touched on the top of their heads, ears, legs, paws, and tail, so take note of these areas.
We recommend slow petting rather than aggressive petting - when you pet the dog in fast strokes, he could get super excited and might playfully bite you. Gentle, relaxed petting will put the dog in a relaxed state: imagine giving him a gentle massage or a light scratch. He'll surely love it as it will calm him down. Discern the places where your dog wants to be handled and pet that area. This will strengthen your bond with your dog, and he will surely love you back! Remember that petting should benefit both the dog and person, so when you pet him, make sure you get relaxed too!
Here is a very helpful video from rover.com