How does voltage know
As you may anticipate, the electrons don't actually know anything. Voltage is actually a battery's difference of potentials of its poles to drive electrons. You may think it as its hydraulic counterpart: pressure. Such that pressure drives the water through tight and wide pipes at a certain flow rate, voltage drives electrons through resistance with a certain current.

If you are interested in a more "scientific" explanation, this is what actually happens:


You connect the battery, complete the circuit
The electric field starts to propagate inside the wires, exerting force on electrons while doing so
Some materials show more resistance to electrons and thus slow them down
The slowed down electrons apply their own electric field behind them, taking away some of their potential to move on.