I used a combination of management and self-control/attention training. Training for this doesn't happen in a few sessions rather it is a gradual long-term process that gets built up over a long time, so in the meantime you would need to use management to prevent your dog from approaching other dogs without permission. Whenever outdoors, until my dog was consistently listening to me with other dogs in sight, he was always on a leash or a long line so I could prevent him from approaching other dogs if his concentration on me slipped, which in the beginning was most of the time.
When other dogs would appear and he would stare at them from the distance wanting to go visit, we would practice stays and giving me eye contact for very brief times. practice this when the other dogs are still far away so the distraction is not yet so intense that your dog's attention completely goes out the window. As the other dog gets nearer, your dog won't be able to keep concentrating on your command. that's fine, at this point just use the leash or long line to stop him approaching the other dogs until you can ask the other owner if it's OK for them to meet. If it is, I would step in front of my dog to block his view to momentarily lower the distraction level for him, then tell him to sit or hold brief eye contact with you, then wait for him to do it, then reward him by letting him go to the other dog. If the other owner says no, then no harm done, your dog is still prevented by the leash or long line from behaving undesirably and more importantly from getting into the habit of going up to other dogs at his first impulse.
For my dog, the opportunity to meet and play with other dogs is by far the highest value reward, he doesn't care about food if there's other dogs to play with. Therefore using the visiting opportunity as the reward is extremely powerful. With repeated practice, done over many months (maturity also affects the dog's self-control level), his self-control has improved so he could stay focused on me off leash with other dogs nearby at increasingly closer distances. If working with food, you may have to take your dog further away from the other dog to lower your dog's distraction levels so that the food is still reinforcing enough to him.
However I try to offset the number of times my dog has to ignore other dogs, with a greater number of times when he does get to interact with them. He usually gets off-leash dog-to-dog interaction at least once or twice a week which involves playing, as well as on-leash interaction (sniffing but not playing). I think that if a dog never gets to socialize with other dogs, then he may become increasingly distracted and frustrated whenever he sees them which makes it harder to ignore them. For example, today we were off leash in the park playing fetch while another person was walking their dog nearby and approaching our direction. My dog was aware of the other dog and was watching them approach but willingly focused back on me so the other owner and dog were doing there own thing near us, and we were doing our own thing still off leash. (I only let him stay off leash around the other dog because I am confident that this was within my dog's ability to handle because by now we have had a lot of previous practice in this same scenario with the safety net of the long line just in case.) But just yesterday he was at the dog park where he played and socialized for over an hour with about 15 other dogs and they were all running around like maniacs.