If you want to understand the theory, it's probably just as important to understand where the notes are in the key the song is in as well as where they are in the chord.
So, playing a D over a G chord in the key of C. So the 'D' is the 5 of the G chord, but the II interval of the key.