Key signatures show the key in which the music has been written. They are placed at the beginning of the stave.
The pattern of a major scale is: tone - tone - semitone - tone - tone - tone - semitone.
Sharps and flats - known as accidentals - are used so that this pattern can be retained no matter which note you start on. Musical pieces are all based on scales, each scale indicating the notes that are available from that key for the composer to use. If notes are also used from outside the key, this is known as chromatic writing.