The parts
Pin 8 on the Uno's ATmega328P chip can work as an output. Inside, it acts like a switch that joins the pin to 5 volts or to 0 volts, called ground. At power on it joins neither: every pin starts as an input.
We wire pin 8 to a 330 Ω resistor, then a red LED, then ground. An LED, a light-emitting diode, glows only when current flows through it the right way: in at the long leg (anode), out at the short leg (cathode).
Switching it on
pinMode(8, OUTPUT) writes a 1 into DDRB0, a bit in the direction register. A register is a tiny memory cell wired to hardware. The switch now works. PORTB0, the output bit, is still 0, so the pin is held at 0 V.
digitalWrite(8, HIGH) writes 1 into PORTB0. The switch joins pin 8 to 5 V. With 5 V at one end of the circuit and 0 V at the other, current flows through the resistor and the LED into ground, and the LED glows.
Sizing the resistor
The LED takes about 1.9 V, its forward voltage, leaving 5 − 1.9 = 3.1 V for the resistor. Ohm's law: I = 3.1 V ÷ 330 Ω ≈ 9.4 mA. The ammeter (a current meter) reads 9.4 mA, below the pin's 20 mA rating.
To choose R, divide the spare volts by the current wanted: 3.1 V ÷ 10 mA = 310 Ω, so use the next standard size, 330 Ω. With no resistor, only the driver limits the current, which can pass 40 mA and damage the pin.
Switching it off
delay(5000) waits five seconds. Then digitalWrite(8, LOW) writes 0 into PORTB0, and the switch joins pin 8 to 0 V. Both ends of the circuit are now at 0 V, so no current flows and the LED goes dark.
What happens
;; step 1/7 · pin board Arduino Uno R3, ATmega328P, 5 V pin 8 port B, bit 0 (PB0) output a switch: pin -> 5 V (HIGH) or pin -> 0 V (LOW) reset the switch joins neither: the pin is an input
In the Arduino core, pinMode(pin, OUTPUT) only sets the DDRB0 bit. PORTB0 keeps its value, which is 0 after reset, so the pin starts LOW. digitalWrite then sets or clears PORTB0. Under load the pin gives a little less than 5 V: the datasheet guarantees at least 4.2 V at 20 mA (85 °C), and LEDs vary (this one is 1.9 V typical, 2.3 V maximum). So the real current is close to, and slightly below, 9.4 mA.