This might be an obvious question, but it would be good to know what factors cause guitars to go out of tune.
There can be a lot of these (others have already hit some of these):
Changes in temperature and humidity. As the wood moves, so does the tension on the neck and consequently the strings.
Nut sticking. If you have a rough nut then sometimes strings can get stuck, causing them to be out of tune. One way to fix this is to down tune the string half a step, then carefully tune it back up moving slowly. Ultimately you want to get the root problem for this one fixed though.
General string post slippage. Depending on how you attach your strings to the posts, they could be slipping there as you tune them up. In extreme cases they could slip off the posts. Research a good technique for attaching string securely to counter this.
New string post slippage This one's related to the other one. The strings aren't actually stretching in this case, it's usually just settling in on the string posts and the bridge. Make sure you stretch them out gently and retune after installing new strings.
Tuning machine quality Poorly manufactured tuning machines can cause string slippage as the gear assemblies move.
- The strings stretch over time. They stretch less and less as time goes on; you'll notice that the older strings are, the less they need to be tuned.
- Loosening of the tuning pegs, and that sort of thing. This can be caused by the tension on the strings, but usually it's due to minor bumps of the guitar and vibration.
- Temperature changes and humidity. Both affect the size (and warp) of various parts of the guitar, which changes the tension of the strings.
- Warping (normally very minor). Wooden parts of the guitar especially bend over time, again changing the tension on the strings.
I think Mathew has hit most of the relevant points, but to add one more thing: Guitar Quality.
Cheap tuning pegs will cause a guitar to go out of tune constantly. The better the pegs, the less it will go out of tune.
I believe it's because the constant tension and temperature changes in the strings slowly unwinds the tuning keys.
The strings may not have been stretched properly when they were put on.
Tremolos are also infamous for wreaking havoc with your tuning.