When a piece is built on a major or minor scale, the tonic of this scale becomes the tonal center. The piece is based on this note: the piece is in the tonality or key related to this scale. For instance, if the scale is a major scale of D, the key is D major.
In the musical pieces written during the Baroque, Classical, and Romantic period, key is meant as the main key. However, numerous modulations (momentary changes of keys) occur throughout the piece.