A pin that listens
A GPIO pin (general-purpose input/output) can drive or listen. pinMode(2, INPUT) makes pin 2 listen. An input only senses a voltage: it lets less than 1 µA flow, so it hardly disturbs what it measures.
A potentiometer is a voltage divider you can turn. Its middle pin gives any voltage from 0 to 5 V. We wire that pin to pin 2, so turning the knob changes the voltage the chip sees.
A digital pin turns a voltage into one bit. At 5 V the datasheet promises: 1.5 V or less reads LOW, 3.0 V or more reads HIGH. These limits are called logic levels.
Reading one bit
Turn the knob to 4.0 V. That is above 3.0 V, so digitalRead(2) returns HIGH, which is 1. The exact voltage is thrown away: 3.2 V and 5 V give the same 1.
Turn it down to 0.8 V. That is below 1.5 V, so digitalRead(2) returns LOW, which is 0. Any voltage from 0 V up to 1.5 V gives the same 0.
Now 2.2 V, between the two lines. The datasheet promises nothing here: one chip may read 1, another 0, and small noise can flip it back and forth. Good designs never leave an input in this zone.
Designing for noise
The knob now plays another 5 V chip's output. Its HIGH is at least 4.2 V: noise can knock off 1.2 V and the pin still reads HIGH. Its LOW is at most 0.9 V: 0.6 V of room. That room is the noise margin.
Unplug the wire and pin 2 floats: nothing sets its voltage. It picks up electrical noise, and digitalRead returns HIGH or LOW at random. The lesson "Pull-up and pull-down resistors" shows the fix.
What happens
;; step 1/8 · input
line 2 pinMode(2, INPUT);
pin 2 input, high impedance ("Hi-Z")
current under 1 µA leaks in or out
INPUT also turns the internal pull-up off
The limits come from the ATmega328P datasheet (DS40002061B, Table 30-1) for VCC from 2.4 V to 5.5 V: VIL at most 0.3 × VCC and VIH at least 0.6 × VCC. The datasheet says "Max" is the highest voltage ensured to read low and "Min." the lowest ensured to read high. Each input passes through a Schmitt trigger (datasheet §14.2.5, Figure 14-2): once it switches, the voltage must move back a little before it switches again. This gap is called hysteresis. The datasheet shows it only as typical curves and guarantees no minimum for ordinary pins. Where a chip switches inside the no-promise zone also varies from chip to chip. The figure's simulated chip switches at 2.2 V with 0.25 V of hysteresis; both are made-up example values, not datasheet figures.