Spectrum in real life with Spectrum in theory as you know it is possible to measure the power spectral density ($|X(f)|^2$) of a certain voltage signal $x(t)$ through a Spectrum Analyzer.
As far as I'm concerned, the simplest spectrum analyzer does not perform the Fourier transform X(f) of the input signal to evaluate the power spectral density. On the contrary, it simply selects each harmonic of the input signal through a tuned filter (or a tuned demodulator) and measures the power of the selected harmonics. The measured power is plotted against all the possible frequencies in the display.
This is what I've seen when I used a 2GHz sine wave as input signal.

It is fine because it is similar to an ideal dirac-delta what I would have expected from theory.
But, there is something I cannot understand. The Fourier transform $X(f)$ (and hence the power spectral density $|X(f)|^2$) is something that may be evaluated only by knowing the full signal behaviour on the infinite time interval $[-\infty;+\infty]$. And the resulting quantities $X(f)$ and $|X(f)|^2$ are obviously constant in time.

However, in real world we cannot know the future and we are sending to the Spectrum Analyzer a real time signal. And it does provide a real-time spectrum, which is not the result of a Fourier Transform evaluation (which wouldn't be possible as we do not know the future values of the signal) but of a simple power measurement.
So, my question is: why does this power measurement (which is an instant function of time: if I stop the input signal, the spectrum becomes 0) coincide with the theoretical power spectral density $|X(f)|^2$ (which is not function of time and which is a pure ideal quantity which requires we to know the signal behaviour also in the future)?
why does this power measurement (which is an instant function of time)

It's NOT a instant function of time.
For example instantaneous power of a sine wave is
$$ p(t) = x(t)^2 = A \cdot cos^2(t) = \frac{A^2}{2}(1+cos(2t))$$
You have to apply some amount of time averaging to get rid of the $cos(2t)$ component and that's what all spectral analyzer do.

: if I stop the input signal, the spectrum becomes 0) ...

Not instantaneously. Since there is time domain averaging involved the power measurement can only change at a finite rate.
In general there is an &quot;uncertainty principle&quot; at work here: the higher the spectral resolution, the slower the system can react or track. For your first implementation, that is determined by the bandwidth of your demodulation filter. The smaller the bandwidth, the slower the output will change.
If you use a Fourier Transform based analyzer, this will be determined by the length of the (finite) time domain window you apply the transform over.

why does this power measurement ... coincide with the theoretical power spectral density |X(f)|2

It doesn't. All physical measurements are approximate in nature. They all have a margin of error, tolerance, repeatability variance etc. You can't measure anything exactly, but only &quot;good enough&quot; for your specific application&quot;.
You also need to define what exactly you want to measure within the context of your specific application and requirements. You don't need the exact power spectrum of an infinitely long signal, since infinitely long signals do not exist in nature.