They have a "map" in their engine management computers. My car (which will be 30 years old next year!) has a fixed distributor. Most petrol cars these days, just have coil packs that go straight on to the spark plugs and get rid of the distributor / rotor arm entirely. Saves weight / cost / space and just works better!
They all have a crank sensor, so they know where TDC is and how fast the crank is turning. From that, the engine management computer can work out when to trigger the coil(s). So, for example, if No. 1 cylinder is coming up to TDC on a firing stroke, at 1000 RPM, it is programmed to send a spark to that plug at that time. Basically, if the crank sensor triggers at TDC on No.1 cylinder, and the computer sees it doing that every 0.06 seconds, it knows the engine is doing 1000 RPM. It can then work out that 10 degrees before TDC will equate to something like sending a spark out (say) 0.598 seconds after it sees the crank sensor pass TDC on the next power stroke. Obviously, as the revs increase, these times will change, but it is a piece of cake for any computer to make those calculations.
It then needs to increase the advance as the revs go up. Again, that's no bother. If it's doing 2000 RPM, it'll pass TDC every 0.03 seconds (and so on), so whoever "maps" the engine will determine how much ignition advance to give it at each different engine speed.
It will also need to alter ignition advance with engine load - which used to be done mechanically with a vacuum advance module, but modern engines will have other ways of determining load - either from a pressure sensor in the inlet manifold, or throttle position, or a combination of signals including the amount of air going through the airflow meter, etc. Again, it can calculate how much earlier than TDC it needs to send a signal to the low tension side of the coil to make the spark arrive at the last time.
Typically, this will be done with many painstaking hours on a rolling road at different engine loads and speeds. Also on a test track.
As engines have become cleverer, they are incorporating other sensors that can help provide information to "fine tune" this basic "map". So, for example, if it has a knock sensor, it can "fine tune" the ignition advance until it just detects the first signs of pinking and then back it off a tiny bit. It can do that every few seconds (or even fraction of a second) if needs be.
The "map" is sometimes called a "look-up" table. The computer will have these basic values in it, and will then use software to interpolate between values for fine tuning. The "Maps" look a bit like this:
http://www.daytona-twintec.com/PC_Link_TC1.gif