There is a lot of question wrapped into one here.
The key signature is indicated at the beginning of a piece on the sheet music. For example a song written in they key of C will have no accidentals, one written in A will have (F#, C#, G#) all indicated at the beginning.
This does not mean that the song must stay in that key throughout. It is very common for music to modulate key. A common pattern is to play a melody in the major scale of the key of a song then move it up to the 4th, Fa, of the same key. Some composers will move the melody without changing key, but will change mode, while others may introduce accidentals in the melody to indicate a true key change from the original, starting on Do, to the key a 4th up, starting on Fa of the original key. Not all scores show the key change. Some will opt to keep the key signature of the song the same and write in accidentals while others will opt to show a new key signature in the sheet music. In fact I have seen pieces written in the key of F or G major but the score shows C as the key. Thus the Bb or F# is written throughout. This is not just in beginner exercises but in professional scores. If the modulation is not going to last long I personally don't need to see the key change, it doesn't really help. In musical scores (scores for Broadway musicals), however, they key may change every few bars so it gets messy to keep digesting new accidentals. In this case an overall key change is more helpful. It's a matter of how the info is presented. You still have to digest new accidentals but if they come one at a time your brain will not comprehend an overall shift in key but when you see the key signature you "know" from years of training what group of accidentals come grouped together and for some instruments the change is just a matter of moving your hand to a new position.
My example of musical score serves to point out that a song can change key as often as the composer likes.
Modes are interesting. There is a relationship between the modes that you may be familiar with. The seven modes are all related to the major scale but starting on different degrees.
On degree 1 of the Major scale --- The Ionian mode (or major scale)
On degree 2 of the Major scale --- The Dorian mode
On degree 3 of the Major scale --- The Phrygian mode
On degree 4 of the Major scale --- The Lydian mode
On degree 5 of the Major scale --- The Mixolydian mode
On degree 6 of the Major scale --- The Aeolian mode (or natural minor scale)
On degree 7 of the Major scale --- The Locrian mode
For example, if you write a melody in D dorian you are really in the key of C major and the sheet music would be written that way. What makes it recognizable as a dorian melody rather than a Major melody is the starting and ending notes of the phrases. A classic example is So What by Miles Davis. This is written in D Dorian.
Now on to the idea of playing in a mode. In classical western music theory and tradition (as it has evolved to in this day and age) it is not wrong to say that songs are predominantly written using the major scale or minor (typically Melodic or Harmonic minor rather than natural minor). But when Jazz players call out So What at a jam session they will often say play it in D. They mean as written, in D Dorian. The first time that happened to me I transcribed up to E Dorian thinking that was the correct "Key", i.e. D Major. So, sometimes people do not refer to a song by the key signature but rather by the note on which the song starts or of the mode being used. You can analyze a modal piece from the classical western music theory perspective and that will work but some cultures are not derived from this tradition.
This brings me to another example where "Key" loses meaning, the Blues. Blues breaks the mold in more ways that one. We typically "change key" with every chord in the I--IV--V sequence by using doninant7th chords on each degree of the progression. In theory this would indicate using the corresponding Mixolydian mode at each chord position and some players do this. But many opt for using the MINOR pentatonic scale starting on the I note over all 3 chords and that works too. You are "out of key" strictly speaking but it's an OUT that works. The more useful scale is "the blues" scale which is minor pentatonic with an added b5. Even more useful is the same with a major 3rd added, a very common staple of Pat Martino solos (and other Jazz greats). Here you have a long chromatic run from the b3 up to the 5th. When blues players call out a tune they might say "The Blues in Bb". This usually (but not always) starts on Bb7 which is, strictly speaking, in the key of Eb major. Written out in sheet music it may very well show the key of Eb but that's not how we think of the blues. The blues in Bb is probably written in a Bb minor mode or scale and played over an Eb major chord structure that modulates key with every, or almost every, change.
One could apply western classical music theory to the blues and map out all those changes and indicate them in a score with appropriate key signatures and whatnot. But in the end that would probably be more confusing for a player who knows the blues tradition.
Not all music is western classical and even tough the rules of music theory and harmony theory are useful there are exceptions to those rules.