It's always a good idea to ask a dog owner for permission before you pet their dog. There are many reasons why an owner may not want people interacting with their pet. Recently our dog was in training and it was really important for us to encourage appropriate behavior with her and avoid too much excitement. We really appreciated being asked before someone touched or interacted with her.
Another thing to consider is that a dog may be prone to biting. This could be due to many reasons such as pain, disability, being elderly, if the dog is uncomfortable, etc. Some conscientious owners will attach a yellow ribbon to the leash of the dog as a signal to ask before petting. This is also a sign to you, if you're walking your own dog, to give the ribboned dog some space. So next time you see a yellow ribbon attached to a leash or collar, you'll know what to do—give this pet some space!