What you have discovered is that there are Diatonic chords for each key.
A diatonic chord is simply a chord that is made up of notes that are in the key.
So in the key of C major you can make the following chords from just notes that are in the key:
C Maj, d min, e min, F maj, G Maj, a min, and b diminished. (you could extend this further by adding the next 3rd up to get all the diatonic 7th chords.)
The relationship for all major keys will be the same: Maj, min, min, Maj, Maj, min, Dim.
In the key of G major the diatonic chords are G Maj, a min, b min, C Maj, D Maj, e min, f# dim.
(the diagram above is not C major, but G major)
On the 7th fret for example you have, on the highest 3 strings (3, 5, 7) which is a B minor triad, diatonic to the key (of G), it is the 3rd chord, the iii (3 minor)! If you kept looking you would actually find shapes for all of the diatonic triads.
Now take your "caged shapes", lets start with the A major shape on the 2, 3, 4 strings (b,g,d strings). If you find the A shape on the 7th fret you have a D major triad (which is diatonic to the key of G (D major is the V or 5th).
Now take your D shape on the top three string. On the 2nd fret it is a D, on the 4th fret it is a E, 5th fret it is a F and then on the 7th fret it is a G! Boom, there it is, in your diagram as a G major which is clearly diatonic as it is the I (1 major).
If you want to play a chord that is not in the key, you can do that by playing a known shape but just know that you are playing notes that are not marked in your diagram above. For example you can play d minor (not in the key of G) by taking your D major chord (using the A shape in my example above, 7th fret on the g,b,d strings) and flat the f# on the b string, 7th fret moved to the 6th fret), notice that this minor is the same shape that you would play an open A minor chord just slid up to the 7th fret, just like we played the D major with the A major shape slid up to the 7th fret.
One more point of clarification: The chord you are playing at the moment doesn't change the key you are in, since each key has many chords. What key you are in has more to do with the tonal center, where you feel the song is at rest (G in G major for example). You can play A minor > D major > G major and you will feel strongly that you were in the key of G since that is a ii > V > I cadence.