The key signature is based on the key of the piece of music, which is the note that the music centers around. The sharps or flats in the key signature indicate which notes should be raised or lowered in pitch by a half step in order to fit the key.
For example, a piece of music in the key of C major has no sharps or flats in its key signature, because C major is a natural key with no alterations. A piece of music in the key of G major, on the other hand, has one sharp in its key signature, indicating that the note F should be raised by a half step to F♯.
Key signatures are essential for musicians to know in order to play music correctly. They provide information about the tonality of the music and the notes that should be used.