A form of abbreviation in music notation, not just a way of notating the scale (or scales) a composition is in. Think of a lengthy work for a string quartet. Not a lot can fit in a single page, right? So, you’d need quite a few pages to write the entire composition. Now, think that we go back in time, before printing; back when books were a commodity and written by hand. Think of all the parchment, all the ink that would go to waste. And on top of that, think how hard it would be to read every single note with a sharp before it, in the occasion the composition was in C sharp major. It would be a nightmare…
So, right after the clef and before the time signature, we have the key signature. The key signature can have sharps or flats. (It can also have naturals, in case the previous section of the composition is written in a scale that contains more flats or sharps than the current one. It’s like resetting the notes back to their ‘default’ frequencies.) Now, what do the flats or sharps mean and where are they located? Simple. Depending on the key signature’s position on their stave, each corresponding note will always be a sharp, a flat or a natural (depending on the symbol), unless instructed otherwise within the score.
If you notice closely, the flat symbol (♭) has an enclosed section. Depending on which line or space that enclosed section fall into, we know which note we should flatten. Similarly, if you notice closely, the sharp symbol (♯) has another enclosed section and depending on its position on the stave, we sharpen the appropriate note. Same rule applies for the natural symbol (♮).
To sum up, key signature makes things easier for us musicians and saves us some time and money (especially back in the day). In case that was too much and you started smelling something burning… and happen to fall into the category of knowing a thing or two about PCs and programming, think this: key signature follows the same concept as CSS does, while the accidentals are plain HTML. Do you see it now?