Let's try to delve deeper into this topic...
Today we know and frequently use the major mode and the minor mode, which is subdivided into melodic minor and harmonic minor modes. However, in antiquity, and specifically in Gregorian chants, many more modes were used. The concept of a scale obtained from a note by proceeding by step up to the same note of the upper octave is an ancient concept. What has changed from yesterday to today is the number of divisions of tones and semitones within the octave that constitutes the scale. In particular, today we know 2 major modes for 30 keys, for a total of 90 major, harmonic minor, and melodic minor scales, not excluding homophonic keys. In antiquity, there were 7 modes for every major key and 7 modes for every harmonic and melodic minor key, for a total of 630 scales, not excluding homophonic keys. Sensitivity to sound was once much greater than it is today. Indeed, each mode evoked a feeling in the soul of both the listener and the composer, and the birth of these modes was due precisely to this need as well as to logical-mathematical evidence. But what are these modes? Let's analyze them together...
Modal diatonic scales built on the major scale (for simplicity, C major)
Let us therefore consider the key of C Major
Starting from the tonic, we find the Ionian mode or major mode (the one most common to us)
W W H W W W H
Starting from the subtonic or supertonic, we find the Dorian mode
W H W W W H W
Starting from the mediant, we find the Phrygian mode
H W W W H W W
Starting from the subdominant or super-mediant, we find the Lydian mode (modernly renamed Neapolitan) and, regarding the sensations we spoke of earlier, I invite you to play it to hear its effect.
W W W H W W H
Starting from the dominant, we find the Mixolydian mode
W W H W W H W
Starting from the superdominant, we find the Aeolian mode
W H W W H W W
Finally, starting from the leading tone, we have the Locrian mode, also called hypophrygian
Summarizing with the degrees, we have this situation
1. W W H W W W H
2. W H W W W H W
3. H W W W H W W
4. W W W H W W H
5. W W H W W H W
6. W H W W H W W
7. H W W H W W W
As one can easily argue, the position of the semitone changes in different modes; however, this changes the harmonic relationships within the scale. In essence, it is true that they are built starting from the degrees of a scale in a key, but effectively these starting degrees are considered tonics for the new mode, which is why we obtain a change in the harmonic relationships. Let's see it in detail...
Ionian 1 2M 3M 4m 5m 6M 7M 8
Dorian 1 2M 3m 4m 5m 6M 7m 8
Phrygian 1 2m 3m 4m 5m 6m 7m 8
Lydian 1 2M 3M 4# 5m 6M 7M 8
Mixolydian 1 2M 3M 4m 5m 6M 7m 8
Aeolian 1 2M 3m 4m 5m 6m 7m 8
Locrian 1 2m 3m 4m 5b 6m 7m 8
In the diagram, let's see how the harmonic relationships within the scale between the tonic (acquired) and the other degrees of the new mode change for each subsequent mode. The exact same procedure should be applied to all major keys and to all harmonic and melodic minor keys.
At this point you will ask yourself: if they were used in Gregorian chants, what sense does it make for us to use these modes today?
The point is that, as you well said, these scales can be used in improvisation; in fact, some of these scales are used in jazz, while other times they are used to avoid the necessity of having voices move in a mandatory way. For example, as in the example you posted, an altered seventh degree must always ascend to the tonic; however, we can use the Mixolydian mode which, as you see, has a relationship with the tonic of minor seventh, therefore not having the altered seventh degree, it may not resolve on the tonic and can move elsewhere. In classical harmony, secondary dominants are often found...
In short, harmonic rules aside, it is always the composer's spirit that suggests the right harmonic methods to apply when composing a piece.
I hope your ideas have become a bit clearer...
Regards