This magic happens with sys.prefix.
Note: If a virtual environment is in effect, this value will be changed in site.py to point to the virtual environment. The value for the Python installation will still be available, via base_prefix.
The site module is imported (from system path!) at interpreter startup, and the site-packages dirs are appended to sys.path with the sys.prefix.
You can verify this for yourself by executing the python REPL with the -S flag to disable importing the site module. You'll find that packages installed in the virtualenv are no longer visible by import statements (assuming they aren't already installed in system site-packages).
Your next question is probably "But how does site itself know if we're in a venv or not?" and the answer is heuristic:
A virtual environment is a directory tree which contains Python executable files and other files which indicate that it is a virtual environment.
If a file named "pyvenv.cfg" exists one directory above sys.executable,
sys.prefix and sys.exec_prefix are set to that directory. Implemented here.