First of all, this is a mathematically undecidable problem, as in that there exist tunes that could be in multiple scales. The extreme example is a melody that consists of only a single note. Ridiculous, but as a fellow nerd I'm sure you can see how I could call a single note a melody. So we have to lose the phase metaphor here a little bit.
Most songs "anchor" with the last note. If you really can't tell whether a song is in major or minor (songs in major are a bit happier, jollier; songs in minor a bit sadder and more melancholic), going for the very last note (or the last note of the chorus, if the song a chorus) is a very good bet. Find the core melody of the song, the thing everything is hung up on, the last note of that core melody is the ground note of the scale. This is a really safe bet.
There are, of course, exceptions, and then I think you'll need to do some mathematical analysis to find out which key the notes in the melody match the best to. I'm not 100% sure how this is defined but I'm sure people have researched it.
All that said, musicians don't really have this problem. After all, hardly any of this stuff was designed. The only genius piece of engineering in all this was the discovery that if you choose to 12 notes in an octave (and not 8 or 15 or whatever), you have a very versatile instrument that can play almost any melody at any pitch in a way that sounds pretty good to human ears (pretty good, not perfect, because of the all the ≈'s in the "Intervals" section of Eevee's article).
But the rest was discovered, not designed, just by fooling around. The analysis came after the music.
Musicians just start with a scale and then make the music they compose fit. This comes pretty natural to you with a bit of practice; most people have a pretty decent innate ability to hear which melodies "match" with a chord and which chords "match" with a previous chord. In all honesty, once you've understood things this far, I'd recommend fooling around with the easiest instrument or tool you know, rather than diving even deeper in to the mathematics of things :-)
I got your single note melody right here:
Strictly speaking, a "note" cannot define a key. To determine a key, you really need at least 2 intervals, wherein interval a somehow resolves to interval b. Keys are just a mapping of relationships; how does each of the 12 notes relate to each other? If you just play a C major chord, you aren't really playing in the key of CMaj, you're just playing a CMaj chord.
For example: Play B+G, then C+G. This is (arguably) C major. The B note resolves into the C note. That's the 7th moving into the Tonic (#1). This is an example of a V-I resolution, the strongest possible. If you'd like more examples, look up "cadences". There are 'principles' for determining the key, but they should not be understood as proofs.
I think you're also asking if/how to change keys. This is not only possible, but often highly encouraged. There are two extremely common key changes in pop music: 1) Up one step: This is incredibly common and can be heard in "I Will Always Love You" (album version, right after the 3 minute mark when she starts singing the chorus again). Once you start hearing this, you can't stop. It's all over the place. If we're in the key of C, we'll just play a D chord with a lot of confidence, typically after a G chord. 2) Minor to Major (or reverse, moving from sine to cosine in the analogy): This is much more subtle than the previous example. Typically you'll recognize this as a change in 'mood' or 'feeling'. You'll find a lot of Am-Em changes, then the chorus will be a lot of C-G changes. The tonic changes from Am to C, even though all the chords and notes being used are still the same (although the duration of said chords will probably be different).
Most "classical" music moves around keys pretty frequently, in extreme cases multiple times within a measure. And then there are chords/sections in which the key is debatable if not indiscernible (I would argue that the intro to Smashing Pumpkins "1979" is changing keys every 2 measures, but I think there's also a strong argument for it being in a single key)
Thinking about keys in terms of phases is a good basic explanation, but ultimately phase is much easier to measure. Perhaps I see it this way because my understanding of music is much better than my understanding of physics.
I've been composing pop music for a long time without knowing stuff like this.
B+G followed by C+G is most likely going to sound like a cadence in C Major. You could claim it is G major only if you considered it unresolved transition to the subdominant.
No it isn't. If you put any piece of music that is harmonious through a device that analyses the frequencies you'll find three main notes and those give you the key. In C-major for example the main notes are C, E and G. The section will be in C even if the first note is E for example.
You can do this with software. There's a nice video of this here https://youtu.be/xVmkIznjUPE?t=24s
or you can upload a recording to get the chords https://chordify.net/
You can indeed fourier transform the music. I imagine the above software does so.
Follow-up question: how are phase of the scale and the piece of music synchronized? When I think in terms of a wave, I could Fourier-Transform it into sines or in cosines, or any other phase-shifted variant (a * sin(nx + const.))?
Is it always the first note of the piece of music that "anchors" the piece in its scale?
Does that question even make sense? :-)