I try to avoid places where off-leash dogs are known/likely to be a problem.
Then I keep aware of my surroundings, and use my dog’s training.
Best case: I ask him to heel and look at me, and then move calmly but quickly past the area with the off-leash dogs. The lack of a reaction from me or my dog often defuses things and the dog leaves us alone.
If the dog starts to approach, I put my dog into a sit-stay or down-stay, preferably facing away from the approaching dog. I then put myself between them and tell off the other dog (NO! Leave it! Go HOME!) with clearly don’t-mess-with-me body language.
I’ve never had to, but if I felt that I or my dog was in danger, I would kick the snot out of the dog until it retreated. Never had to do that, yet.