Advances in guitar manufacturing has solved many problems with guitar tuning, but guitars are still extremely sensitive to their environment. If you tune in a room with a set temperature and humidity, then take the guitar into another room that is hotter/colder and/or more/less humid, some guitars will go out of tune. When the guitar experiences a change in humidity or temperature, it causes the wood and strings to expand or contract. It is best to let the guitar acclimate itself in the room in which it will be played then make adjustments and re-tune it. Extreme changes in humidity or temperature can damage your guitar with warping, especially in the neck area, so you should treat it with proper care. If you have a guitar neck that is warped, some structural aspects of the guitar, like the neck, fretboard or truss rod, can be adjusted. See the Adjusting the Guitar appendix.
When fresh strings have been put on a guitar, they will often fall out of tune very easily. This is because after having been put on the guitar, the strings still have a lot of slack. It will take time to work all the slack out of the string, but the process can be sped up somewhat. After the strings have been put on, loosen them a fair bit and then bend the string gently. Turn the tuning peg up, and then bend the string again. After this, strum chords enthusiastically for a few minutes and tune again. Most of the slack should be gone from the strings, and the guitar should stay in tune.
Modern instruments use equal temperament tuning, and the guitar is no exception. Older methods of tuning have the problem of certain intervals sounding out of tune, while others did not. Equal temperament tuning is a compromise between older methods, and has the smoothest overall sound.
en.wikibooks.org/wiki/Guitar/Setting_up_the_Guitar