How does attaching analog inputs to digital pins actually works?

A digital input has a threshold voltage and hysteresis.

The values depend on the supply voltage. According to the data sheet for the ATMega328P (Uno), for 5V supply, anything above about 2.6V is considered HIGH, below about 1.8V is LOW.