Part of being a great guitar player is knowing what to do if your guitar won't stay in tune. There are a number of causes for intonation problems – and a solution for each.
Old strings. Sometimes all that's needed to fix your tuning problems is a new set of stings. As guitar strings age, the oil from your hands attracts dust and dirt which causes the strings to vibrate unevenly and lose their elasticity (the ability to be stretched). Plus, when the surface of a string collects dirt or becomes thinner than normal in places, it simply doesn't vibrate consistently and accurately as it did when it was new and thus sounds out of tune. Strings can be cleaned with products from your local music store but the ultimate solution is installing new strings every six months or so.
The bridge. This is where most intonation problems occur. Whether you have a fixed bridge (most acoustic guitars) or an adjustable bridge (most electric guitars) by theory, the distance between the guitar's nut (the slotted piece at the first fret) and the 12th fret should be exactly the same as the distance from the 12th fret to the bridge. With a little knowledge and practice, you can learn how to adjust the bridge so the guitar stays in tune. Many guitar bridges allow you to adjust the strings individually while fixed bridges must be adjusted as a whole. Tip: Use a quality electronic guitar tuner when making adjustments (or let your local guitar repair professional adjust the bridge for you).
The nut. The guitar nut (the slotted piece where the strings rest as they make their way to the tuning machines) can cause intonation problems if any or all of the six slots are not the proper depth and width. If the string sits too high for example, the string will not fret in tune, especially at the first two or three frets. If the slot is too big and / or two low, the strings will buzz and make tuning difficult. Some players and guitar techs use graphite powder or grease at the nut to ensure the strings move freely when bent or tuned. This little secret helps keep the guitar in tune and makes tuning the guitar a lot easier.
Using a capo. It's a bit tricky to put a capo on without changing the intonation of your strings. The act of placing the spring loaded or cam-type capo can easily bend one or more of the strings. The solution? Place the capo straight down on the strings and tighten it slowly, watching to make sure you don't bend or distort the strings. Practice this until all strings stay in tune when the capo is used.
Tremolo arm. Unfortunately, very few guitars stay in tune when their tremolo bar is used. Even one push, one "wow" effect, can have an adverse effect on the strings. In other words, when they come back to pitch after the tremolo bar is released, the stretching causes them to detune. There are guitars, although they can be a bit pricey, where the strings maintain their pitch no matter how many times the tremolo is used. Some of them have a locking nut where the strings are clamped down allowing them to return to proper pitch.
With a bit of knowledge and practice, you can eliminate tuning problems with your guitar and focus on making great-sounding guitar music.