Major and natural minor scales built on C and A respectively do not contain any altered note. In order to build these scales starting from any other note, it is necessary to alter one or more notes. For instance, in the scale of G major, note F is sharp. If you want to write a melody in G major, you would need to alter all F notes. In order to reduce the number of accidentals at the time of writing music we resort to the use of key signatures. These accidentals, written between the key and the time signature affect all of the notes of that name throughout the piece, including those in other octaves.
Key signatures are placed at the beginning of each staff, between the clef and the meter signature:
In the former melodic fragment, all F are sharp. Therefore, if you want to write a natural F, it should be preceded by a natural.
Scales with sharps in their key signatures are the following:
Scales with flats in their key signatures are the following: