For historical reasons, a musical scale starts on a note (called the tonic or root) and progresses up in 7 steps (or degrees) to a note with the same name. The 8 notes give it the name octave and a scale in 8 notes is called Diatonic. This is the normal state of affairs in the majority of Western music.
(For interest, a scale of 12 notes is called Chromatic, and a scale of 5 notes is called Pentatonic. Pentatonic scales are much favoured in the Orient and chromatic scales are favoured by the avant garde)
Modern instruments have 12 notes (semitones) in the octave, not just 8. i.e. 12 keys on a piano (the black notes are the extras) and 12 frets on a mandolin (banjolin, banjo, etc) between any 2 notes of the same name. Contrast this with a tin whistle or a melodeon where the scales are fixed - a D whistle or a D - G melodeon (2 keys). This means that modern instruments are multi-purpose - they can play any scale although you generally play a permutation - any 8 from 12 (well certain 8's from 12 anyway). Of course they can play any other type of scale (Chromatic, Pentatonic etc. )because all 12 semitones are available.
Starting a major scale on a note other than C (or a minor scale on a note other than A) needs notes that are not there in the natural key (i.e. C and A minor). The starting note is called the Tonic (key note) and gives its name to the key.
So the key of G starts and ends on G but playing ...
... will not sound right. The F needs to be sharpened to get the scale sounding correctly in the major mode (as the C scale did) Any scale (major or minor or other mode) is formed by a particular interval of notes. In fact, this 'scale' which is equivalent to playing only white notes on a piano, but starting on a 'G' is the Mixolydian Mode which is quite common in common folk tune.
A major scale (see Ionian) always goes in steps of T-T-S-T-T-T-S (S = semitone, T=tone=2 semitones) so while the scale of the key of C is: CDEFGABC, the scale of the key of G is: G-A-B-C-D-E-F#-G. Note the F# to get the tone interval after the E. So the key of G is '1 sharp' - the F has to be played sharp to fit the major scale intervals.
There is a progression of keys from C and traditionally, half are keys with an increasing number of sharps and the other half have an increasing number of flats. It is possible to think of all keys increasing the number of sharps (up to 11) or increasing the flats to 11 to get all the keys, but convention divides the keys into half and half.
The table below lists all the 12 keys (note that F# and Gb are the same!). Or see the keys page for an alternative view. Starting with C (no # or b), the next key up starts on the 5th note of the scale - which is G (CDEFG). The next starts on the 5th note of the scale of G - which is D (GABCD) but to get the intervals, it has to sharpen the C to C# (DEF#GABC#D).
The new scale (going sharp) always starts on the 5th note of the preceding scale. It also keeps all previous sharps, and always adds a new sharp as the 7th note of the new scale. A handy rule.
Starting with C again, the next key down starts on the 4th note of the the scale which is F (CDEF). To get the major intervals correctly, the key of F has to play the B flattened.
The new scale (going flat) always starts on the 5th note down (or the 4th note of) the previous scale. It keeps previous flats and always adds a new flat which is the 7th of the old scale as the 4th note of the new.
So, the next sharp key starts on the 5th note of the previous key, and the note sharpened is the 7th note of the new key. The next flat key starts on the 4th note of the previous key, and the note flattened is the 4th note of the new key.