As a rule of thumb you should stain a fence every 5 to 7 years depending on the type of wood. We always recommend a Cedar fence. Cedar has natural oils and acids in the wood that make it undesirable to most insects. This allows the wood to be installed in its unfinished form and last for many years. Cedar also warps less than Whitewood or Pine fences.
It’s impossible to completely prevent the natural weathering that occurs to a wood fence over the years, but you can slow the process by protecting the wood regularly from the elements. The stain you use to cover the wood must cover every part of the wood exposed to moisture. It’s also important to use galvanized or stainless steel hardware in the wood because these metals won’t rust and they resist warping.