People often pat the top of their dog's head because it is an easy place for them to reach. Your dog may be used to you doing it but watch his body language. Does he really enjoy it?
When we touch a dog's head we can cover the dogs eyes or ears which they use to be aware and control his surroundings. We can touch the sensitive hairs on their muzzle which can be very distracting and sometimes disorienting to a dog. This can make make the dog very nervous, especially if the dog is a naturally nervous dog already. Depending on the force you are using, it can shake their head and blur their vision.
There are dogs that don't mind this if they are comfortable with the person doing it, but there are dogs, such as the typical shelter dog that just doesn't like it. They may have been abused or have had a bad experience that you just aren't aware of. Therefore, if you do not know a dog very well, it's best not to touch his face. The safest is to touch a dog on his chest or back to not bother him. Leaning over a dog to touch him is not appropriate either because it might make the dog think you want to attack him. When a dog is not sure about a human's intentions he usually moves his head away from the person's hands.
If it's not your dog, a good rule of thumb is to "ask first". Just because you are used to being around friendly dogs, doesn't mean that all dogs are friendly to you. That doesn't mean they are not friendly dogs; they just don't know you. It is always proper to ask to pet someones dog before doing so. Let their dog smell you and get to know your scent before you approach them.

