This cannot be answered shortly (it can but then, it will not be much help). This is the basic idea.
1. Simple pieces are written in one single key, that is all the notes in the piece will come form one single scales. For instance, Twinkle Twinkle little star uses only the notes from the sale of C major.
2. If you now take the scale of C major and build a triad on each note of the scale, you get the triads of C major (CEG), D minor (DFA), E minor (EGB), F major (FAC), G major (GBD), A minor (ACE) and a tritone on B (BDF). Let us forget about the tritone for now.
3. So you get 3 major chords and 3 minor chords. In fact they are the relative minors of the major chords, i.e., C major/A minor; F major/D minor and G major/E minor.
4. If you now look at these scales, you will see that they share with C major all the notes but one (G major has an F# instead of an F and F major has a Bb instead of a B – their relative minors have the same notes). This means that these keys, F and G are very similar, very close to the key of C major.
5. On the other hand, if you look at a scale like B major, it shares almost no notes with C major (only the B and the E). So B major is a very distant key from C major.
6. If you want to change keys, to start with try to change to keys that are very close to the original key. It is not impossible to change to distant keys, but if you do not do it properly it will not sound good.
7. To change from one key signature to another, all you have to do is change the relevant notes. For instance, if you are playing a melody in C major, and you start playing F# instead of F, you have changed to the key of G major. If add a Bb in place of a B, you have modulated to F major.
8. However if you do that suddenly the results will not be nice. So you must make the transition smoothly. For instance, if you are playing a piece in C major, and you just played the chord of C (CEG), and you want to change to the key of G major by playing a G major chord (GBD). As you can see only the G is common to both chords. You must find an intermediate chord that will allow a more smooth transition, in short, a chord that has more notes in common with both chords, for instance, E minor (which is one of the chords you can derive from C major). If you now play C major (CEG) and follow with Em (EGB) you can see that two of the notes are common (EG). If you now follow with G major (GBD), again two of the notes are common (GB). Got the idea? You can faind these things by experimentation and careful thought (lots of comparisons), or you can get a book that has the most common smooth transitions (there are several. Try Gail Smith’s “Book of modulations for the pianist” – Mel Bay). This sort of book will have – worked out for you all the intermediate chords that will make a smooth transition from a key to any other key no matter how distant. I do suggest that you try by yourself with the chords within a scale so you get the hang of the basic idea before jumping to more distant keys.
I hope this helps.
Best wishes,
Bernhard.