Let's talk about your guitar and playing in tune anywhere on the fretboard. By playing in tune we mean each string is tuned correctly in its open position and each note played on the fretboard is also in tune in every fret position. If your guitar is tuned correctly in the open position for all strings but still plays out of tune anywhere on the fretboard, you need to make sure the intonation for each string is set properly. If you look at your guitar and locate the bridge, you will notice the bridge is set at an angle and each string is lined up slightly off from the string next to it. The bridge has adjustable saddles to where you can dial in each string to have perfect pitch within itself. You should be able to play an "E" in the open position and the same string at the fifth fret should give you an "A" and so on up the fretboard. If any of the individual notes are off, the intonation needs to be checked and adjusted accordingly.
Keep in mind that the gauge of your strings can also affect your intonation. If you are using a set of strings all with their specific thicknesses and you decide to change strings and go with a lighter or heavier set than what was previously on your guitar, you should re-check your intonation. Different gauge strings will affect your intonation and make it slightly off again. You need to check this and adjust the saddles accordingly.
In extreme cases, where you simply cannot get your guitar in tune and/or the intonation correct, check your neck for back bow or any other issues. If your guitar is older and has been played a lot it might need a neck adjustment and a fret leveling or even new frets. It is a good idea to have your guitar checked by a professional every couple of years. This will ensure your guitar is at the optimal playing condition. You wont waste your time fighting tuning issues but rather more time playing and being creative.
Change your strings often, have your guitar checked every two years and keep it in the case when not in use and you should be able to enjoy your instrument for a lifetime.