It's based on scales. Each note in the scale has chords that are associated with it. The triads for the I, IV and V notes in a scale are all major chords. The triads for the II, III, VI are all minor chords. The triad for the VII is a diminished chord.
So, if you know the scales well, you can always find the IV and the V. Simply play the major chord with that note as the root.
For example, in the key of G.
The G major scale is
G A B C D E F# G
The 1st, 4th and 5th notes of that scale are G, C and D.
So the chords you'd play are G major, C major, and D major.
Scales are at the root of everything.
Someday we'll win this thing...