Modulation is the process of changing key center. Modulation occurs in a number of ways, most of them involving some way of making the current key ambiguous and reaching a cadence in a new key. The key chosen is usually one closely related to the original key, those that share the key signature or have only one sharp or flat different in the key signature (for example, in C major, A minor, G major, and F major are all closely related keys). Modulation to other keys is not unknown, however, particularly in the Romantic era.
There are several means used to modulate. One is simply to introduce a new key, though this does not always sound very convincing and many consider it quite crude. In classical music, modulation is usually prepared and carried out through the use of pivot chords, which exist in both the starting key and the intended destination. Especially during the Romantic era, the possibilities of chromatic pivot chords, such as the diminished seventh, were heavily exploited.
A pivot chord is a chord that is common to both the key you are moving from and the key you are moving into. The pivot chord begins as a particular chord in the original key (eg. iv) and is then used as a different chord in the new key (eg. ii). This creates a smooth transition to the new key.
Eg. To modulate from C major to G major, the chord a- is common to both keys, and can be used as a pivot chord as follows:
C -> a -> D7 -> G
(I -> vi/ii -> V7 -> I)
Another method of modulation is to treat a major chord in a given key as the dominant chord in a new key by adding a minor 7th above. Commonly, the tonic chord is used like this to modulate to the sub-dominant:
Eg. in C major, modulating to F major
C -> C7 -> F
(I -> I7/V7 -> I)
Or to modulate to Bb, one may follow this progression:
C -> F -> F7 -> Bb
(I -> IV -> IV7/V7 -> I)
Uses of modulation
Modulation is integral to sonata form, where a modulation to a (usually) related key in the exposition sets up an important tonal contrast in the development. Modulation is also used in other forms like the fugue and rondo to create contrast within unity.