Learning how to stop a dog from digging essentially requires an observation of your dog's behavior, and an understanding of the underlying motives. It is important to remember that digging is a natural behavior for dogs, so you are going to have to work with your dog to achieve a mutually satisfying outcome to the problem. If your dog is digging primarily out of instinct, the best solution is to give him a space where it is okay to dig. Encourage him to dig there, teaching the commands "okay" and "no," to distinguish between acceptable and non-acceptable digging. Try hiding some toys or treats in the designated dig zone, and repeat this daily until this behavior becomes established.
Other motives you need to understand in order to stop a dog from digging, include mental factors: stress, anxiety, boredom, loneliness, seeking comfort or shelter, or trying to escape. All are common reasons for digging, depending on the dog's breed, temperament and lifestyle. Terrier dogs, whose name means "to bury into," are especially prone to digging. One of the reasons punishment is not recommended as a strategy to stop behaviors like digging is that increased anxiety can just increase unwanted behaviors in your dog, as a kind of coping mechanism. Remember that dogs are very much like children, in that reckless punishment without explanation, will only make them more rebellious than before. It is much better to understand the reasons for behavior issues, and work from there.
If a pet is bored or lonely, they are not getting enough stimulation and you need to provide more activities for them. Dogs require regular interaction with their people, and sometimes with other dogs as well. Make sure you are spending time with your dog, and getting enough exercise to keep you both happy and healthy. Take your dog for plenty of walks, and socialize him with other dogs at a trainings school or dog park. This should diminish digging for reasons of both boredom and escape.
A dog may dig herself a shelter if she needs a retreat from the heat or cold, or out of a nesting instinct. The best way to dissuade this kind of digging is to make sure your pet has access to a comfortable environment, either in an insulated doghouse, or a shaded area where it can get relief from the sun. Even better, provide a small kiddy pool your dog can bathe in to cool down. Most dogs love water, so by providing a small pool you are both cooling it down and giving it some mental enjoyment. Make sure their shelter is protected both from direct sun and wind and that they always have fresh drinking water available.
Figuring out how to stop a dog from digging doesn't have to have you pulling out your hair! There are plenty of resources available, and most advocate the development of good communication between you and your dog, which will yield unending positive results in your life together. Be patient as you and your dog find ways to accommodate one another and enjoy the experience.