There are several reasons why a piano may go out of tune. No piano can be in perfect tune for more than 6 months, sometimes even less, even when it's not being played. Therefore, one must tune their piano at least every 6 months (the reason why is given below). The reasons for the piano going out of tune are:
1. Tension in the strings
The different strings in the piano are under different levels of tension. The low frequency strings are under lesser tension as compared to the high frequency strings. Also the number of strings per frequency varies, thus adding to the variation in tension across the cast iron plate.
Normally, when you don't play the piano, there is a lot of energy stored in the strings due to the tension, just waiting to be released. This tensile force may give way (a tiny little way, and very gradually) and the string becomes ever so slightly less taught (as the frictional force that keeps the string under tension in the pins gives way to the tensile force in the strings).
When you play the piano, as the hammers hit and vibrate the string, this loosening takes place a little faster, especially for the strings being struck.
2. Temperature change
With the main logic being change in tension of the strings and their loosening, this process can be accelerated by changes in temperature. During the summer season, there is linear expansion of the strings, and they become naturally loose even though they do not slip on the pins. During the winter, the temperature drop contracts the strings, thus making them more tight, and they experience more tensile force.
This temperature change is the main reason why pianos go out of tune, and you have to tune them every 6 months, when the seasons change. This is independent of how well the piano is made, and thus why all pianos go out of tune.
3. Humidity change
Though humidity doesn't affect the steel strings much where frequency is concerned (they do cause rusting over many years though, and strings can snap when they become weak), it affects the wood a lot. The steel string vibrating themselves produce a faint sound. The sound you hear is the sound of the wooden soundboard vibrating.
Humidity changes the density and elasticity of the soundboard, thus rendering the piano out of tune. As humidity increases, wood absorbs more water and increases in density and size, and vice versa.
As the moisture level in the soundboard increases during periods of high relative humidity, the crown expands and pushes the bridge harder against the strings. The strings are stretched tighter and the piano’s pitch rises. Because this increase in crown is greater in the center of the soundboard than at the edges, the pitch rises more in the middle octaves than in the bass or treble registers.
4. Other factors
I've also noticed people placing goods inside the piano, especially in grands. This may add extra forces on the string, and even causes the timbre to change (since more than the strings and soundboard are vibrating). Keeping stuff inside the piano is highly not recommended.