Training your dog to stay by your side is an imperative command when you are out and about on leash or off-leash. Having your dog run from you because of a distraction or over-excitement can be quite scary and dangerous. If you are at a dog park or even just on a neighborhood walk where other animals may come across your dog, you want to your dog to remain safe and close to you so you can help protect him. Dogs who walk off-leash often need to know how to stay by their owner's side. Dogs who hike with their owners in public spaces off leash or dogs who go to restaurants or walk around town off-leash are the dogs who are expected to be well-behaved and next to their owners at all times. Training their dogs to remain by their side is the only way these owners can have their dogs completely off-leash.
Training your dog to be off leash and by your side will start with training your dog to be on-leash and by your side. You will want to teach your dog to heel. Whether your goal is for your dog to be on-leash or off-leash all of the time, you're going to need to train your dog to avoid distractions and keep his attention on you. If you move, your expectation should be that he moves with you. If you sit still, your expectation should be that he sits still with you. Though this can be taught at any age, it's often easier to teach puppies and younger dogs from the start how to stay by your side.
Training your dog to stay by your side will require daily small sessions of training. You will want to be consistent in your rules and how you decide to train your dog to stay by your side. You may need a leash to start even if your plan is to have your dog off leash once he is fully trained. High-value treats will help entice your dog to stay with you as well as to reward good behavior. Be patient and build daily training sessions into your days.