Key signatures are groups of either sharps ♯ or flats ♭that you can see at the beginning of a staff, right after the clef (and before the time signature).
They tell us what notes are either raised (♯ ) or lowered (♭) throughout the whole piece.
This key-signature (3 sharps) below, is telling us that in this piece all the F, C and G's should be raised a semitone, or a half step (#):
The tonalities, or scales, that do this are A major and F# minor. So, the key is in A major or F# minor.
Accidentals, in contrast, are sharps ♯, flats ♭ or naturals♮, added in front of the notes wherever they are needed in the piece.
Accidentals last only for that particular note and measure.
Accidentals; sharps, flats and naturals, are written in front of the note where used.
The main reason for using a signature and not simply accidentals, is that it makes the music looks cleaner and easier to read.
The signature represents a certain major or minor scale that the piece is made from. This is also called the tonality or key.
So, in theory- by looking at the signature, a musician knows what scale to use and what sound to expect.
But, in reality it is much more flexible.
Yes in theory they could. However, most often, they are used for pieces that are in major and minor.
Other types of scales and modes may use accidentals and an open signature (like C major):
But, even a piece in major or minor usually changes a lot.
It might modulate (move) to other keys, use different accidentals, and finally not be a major or minor scale at all!
The key can also be changed where needed in the piece. This is called a key change.
But, even though the key signature is very flexible, it still helps us to understand what scale the piece is mostly made from. We also know what chords, or diatonic harmony would be most common to use in this key.
There is a signature for each major and minor scale. Ouff! :)
But, the good news is that one signature represents both a major and its related minor scale. 2 in 1 so to speak.*
If the signature has one sharp, it means it could be either G major or its relative; E minor.
Now check out the last note or bass note of the final chord. This is normally the tonic (the first note of the scale):
This is because most pieces finish on a note/chord that sounds like «home» (tonic).
Courtesy of Encyclopædia Britannica
A major or minor scale can use up to 7 sharps or 7 flats. C major/A minor has no sharps or flats but still counts as a specific key.
So, that makes 7 + 7 + 1= 15 different signatures.
But, the funny thing is that some of those are actually sounding the same!
This is because some of the scales are enharmonic (sounding the same but with different names).
For example B major and Cb major have totally different signatures, but you play exactly the same notes:
So, do you use the B major or Cb major signature when composing?
The answer is; whatever you like (or how nasty you wanna be! :)
However, if you want easier reading, the B major signature is better since it uses only 5 sharps. Cb major uses 7 flats and is a bit awkward to read.