Teaching your dog to stay withing a boundary is actually pretty easy. It consists of many small steps, and can be achieved through many techniques. The techniques I will not be talking about are the ones that include E-collars, shock collars, or any other form of negative association. You want your dog to want to stay in the boundary because when he does good things happens, that way, no matter what the scenario is, your dog will always prefer to stay within the boundary you set.
Some boundaries are harder to teach than others. For one of my dogs, all it took was a few times of him stepping out of the front door and me saying no for him to get the point. But for that same dog it took weeks of training to get him to stay in the yard when I threw a Frisbee into the road.
For me clicker training was the best method to teach this concept to your dog. As with all things, start small and reward for small victories. Stay within the boundary, say a room right by the restricted room in your house, or the front yard, and reward your dog for moving around the area within the boundary. Continue doing this as you step out of the barrier and put more and more distance between you and your dog. If your dog crossed the barrier, you can correct him with a 'no' and guide him back where he should be.
Once your dog has an idea of where the barrier is and consistently stays behind it, start having your dog walk along side you to the barrier, stop, and treat him when he stops as well. Increase the difficulty and treat him just as he reaches the barrier, while you keep walking. Then walk across the barrier and withhold treating him for increased increments of time. Soon you can walk anywhere you like and your dog will stop at the barrier every time your cross.
Here is a fantastic video that helped set me up for success. It does a way better job explaining what to do than words ever could.