When your ball ends up out of bounds, you add one penalty stroke and play again from the spot of your previous shot. That is called stroke and distance: you lose a stroke for the penalty and the distance of the shot you just hit.
What counts as out of bounds (beyond the white stakes)?
Courses usually mark the boundary with white stakes or a white line, so "beyond the white stakes" means out of bounds. Your ball is out of bounds only when all of it is outside the course boundary. If any part of the ball is touching the ground inside the boundary, or hanging over it, the ball is still in play. You are allowed to stand out of bounds to hit a ball that is inside the boundary.
What do I do when my ball is out of bounds?
Take one penalty stroke and play a ball from where you hit the last shot. The penalty counts, so if your tee shot goes out of bounds you are hitting your third shot from the tee. If you are replaying from the tee, you can tee the ball up. Anywhere else, you drop a ball in the right way (see Taking relief: measuring and dropping correctly).
You can also play a provisional ball before you walk forward if you think the first one might be out of bounds. That saves a trip back.
Is there an alternative to going back?
Some leagues use a local rule, called Model Local Rule E-5, that lets you keep moving forward instead. If your league has adopted it, you can skip the walk back. Instead you take two penalty strokes and drop in an area near the fairway, between where the ball went out and the edge of the fairway, no closer to the hole than where the ball went out. Once you play from there, your original ball is out of play even if you find it.
This is a choice your league's committee makes for casual and league play. It is not meant for elite competition. It does not apply to an unplayable ball, or to a ball you know is in a penalty area. Ask your organizer whether your league plays E-5. If nobody has told you it is in effect, assume it is not and use stroke and distance.
The governing rules are Rule 18.2 for out of bounds and Model Local Rule E-5 for the optional alternative.
Local rules
Your league's committee may have local rules that change this. Ask the organizer which ones your league uses.