Dogs are a bit like husbands, in some respects. If they're not happy at home, they may stray in the hope of finding a better life. So the main thing to concentrate on is to make the home environment so attractive, he won't even consider going elsewhere. Make sure he's never bored, and he won't want to stray from home. (This works for both dogs and husbands, incidentally!)
There are other practical measures you can take to keep Fido where he belongs. Be warned - these measures are not suitable for straying husbands - especially neutering and training to sit and stay. Oh, I don't know though ...
Be security conscious
Ensure your garden is secure. Gates should be fitted with reliable catches - you may find a self-closing catch is useful if you have children who are likely to forget to close the gate. Fit a notice asking callers to close the gate, as there is a dog on the premises. This may also act as a deterrent to burglars. Fencing should be well maintained with no gaps, and high enough so that, if Fido fancies his chances in the Grand National, he can't practice by jumping over the garden fence.
Keep external doors closed all the time, or fit a child's safety gate so the dog can't make an escape bid when you answer the door.
If you start early enough, it's possible to train your dog to walk to heel off the lead. You can also train him to sit and stay in a certain place when someone knocks the door, to lessen the chances of escape. To be on the safe side, start this training in your own garden, and progress to the great outdoors when Fido gets the hang of 'sit,' 'stay,' and 'heel.'
Have him neutered
Neutering curbs the instinct to roam, and it's also a good idea if you don't want Fido to bring disgrace to your door after a night on the tiles with the local canine beauties. He won't realise what he's missing, and a neutered dog is less likely to get into fights with other dogs or make a general nuisance of himself.
As a pet owner, it's your responsibility to ensure that your dog is supervised at all times. Stray dogs can cause all sorts of problems to other dogs, pedestrians and road users, and it's relatively easy to train your dog to obey your commands. If only it were as easy to train husbands!