Why do you get instantaneous phase by calculating correlation with a carrier signal? I am somewhat new to DSP. I'm trying to implement BPSK by following this Python QAM tutorial.

My code seems to work, but I really just ported the tutorial's Python code to C++, and do not understand it. This is the part that confuses me the most:

# Demodulate in "real" and "imaginary" parts. The "real" part
# is the correlation with carrier. The "imaginary" part is the
# correlation with carrier offset 90 degrees. Having both values
# allows us to find QAM signal's phase.

for sample in samples:
    t += 1
    angle = CARRIER * t * 2 * math.pi / SAMPLE_RATE
    real.append(sample * math.cos(angle))
    imag.append(sample * -math.sin(angle))

for t in range(0, len(real)):
    # This converts (real, imag) to an angle
    angle = math.atan2(imag[t], real[t])
    phase.append(angle)


I understand that this code is correlating the input signal with two identical carrier waves which are offset by 90º. I do not understand why that correlation has anything to do with phase.

I'd love an answer to any of the following questions:


Why does this code work? 
What is the connection between correlation and phase? 
Is this a common, reliable method of calculating phase? 
Why does the FFT need so many samples to calculate phase, but this code just needs 1 sample?


Feel free to refer me to a signal theory or DSP book that answers these questions!
This is the analytic signal. See wikipedia for better details.

Basically you are taking the carrier correlation, $f(x)$, and its Hilbert transform, $(h \ast f)(x)$ and combining them into a single complex valued signal, the analytic signal

$$f_A(x) = f(x) - i (h \ast f)(x)$$

which can be written as the complex exponential

$$f_A(x) = A(x) e^{i \phi(x)}$$

Why this is useful is that it performs a split of identity into seperate amplitude $A$ and phase $\phi$ components. Actually the Hilbert transform requires the entire signal to compute, but because you know the function of your signal already you only need the one sample.To localise the phase, you would use a bandpass filter.