Key signatures, and the order that never changes
The accidentals gathered at the start of the staff, the fixed order they appear in, and the trick that reads the key in a second.
Writing B major by marking five sharps every time a note appears is possible, and nobody does it. The accidentals that hold for the whole piece are gathered at the start of the staff: that is the key signature.
The order of the sharps
The sharps always enter in this order, and never in another:
F — C — G — D — A — E — B
No key skips one. If it has three sharps, they are F, C and G, in that order, with no alternative.
A major has three sharps: F♯, C♯, G♯. Exactly the first three of the list.
The order of the flats
The flats follow the same list backwards:
B — E — A — D — G — C — F
E flat major has three flats: B♭, E♭, A♭. The first three of the list of flats.
Why the order is what it is
It is not arbitrary convention; it is the circle of fifths.
Go up a from C and you reach G, which needs one sharp. Another fifth and you reach D, which needs two. Each fifth upward adds exactly one sharp, and the new sharp is always the leading tone of the new key.
Going down by fifths, or up by fourths, the mirror image happens: each step adds a flat.
Reading the key signature in a second
Two tricks, and they are the ones musicians actually use:
With sharps: look at the last sharp of the signature and go up a semitone. Last one is F♯? G major. Last one is G♯? A major. The last sharp is always the leading tone, and the leading tone is always a semitone below the tonic.
With flats: look at the second to last flat. It is the tonic. A signature with B♭ and E♭? The second to last is B♭: B♭ major. The only one that escapes is F major, with a single flat, which memorises itself.
One signature, two keys
The same signature serves a major key and its relative minor, a below. No accidentals: C major or A minor. One sharp: G major or E minor.
The same seven sounds, two different centres of gravity. Which of the two is in force cannot be read from the signature; it is read from which chords the music insists on, and which of them it ends on.
What to take from this
The key signature is the scale gathered into the margin. The order of the accidentals comes from the circle of fifths and never changes; the last sharp and the second-to-last flat name the key at a glance. What the signature does not say is whether the piece is major or minor: the music decides that, not the signature.