How does this transistor NOT gate work?
The graph below shows voltage between Base and Emitter of a P2N2222, for Collector currents up to 500mA:-



When the switch is OFF the Base is open-circuit so the transistor will be off and all the current will go to the LED. Depending on the LED type its voltage drop could be between ~1.1V (infrared LED) to ~3.3V (white LED), and current through the 470&ohm; resistor will be somewhere between ~9mA and ~5mA. 

When the switch is ON the Base-Emitter junction will draw current, dropping the voltage to ~0.8V and turning the transistor on. The transistor has current gain so more current will flow through the Collector-Emitter junction which will pull the voltage even lower, but that also reduces Base voltage which would turn the transistor off, so it will settle at ~0.7V with Collector current at ~11mA (corresponding to voltage drop of 6V-0.7V = 5.3V across the 470&ohm; resistor).   

At this lower voltage the LED draws very little current so it is effectively 'off'.