When a piece is not in the key of C major or A minor, it requires the use of accidentals. To avoid having to write these accidentals over and over again, we can place them at the beginning of the piece using what is called a key signature.
For example, a piece in the key of D major regularly uses the notes F# and C#, so the key signature uses these accidentals. When this key signature is present, all F and C notes are automatically raised and become sharp notes, unless the symbol of the natural accidental precedes them.
Following, we have a fragment from Beethoven's famous Ode to Joy, in D Major's key. Note how all the F and C notes are played sharp:
Practice key signatures construction and identification
For more information see:
Translated by Dan Román, revised by Sue Talley.