Basically the ECM stores a preprogrammed map that defines fuel delivery and timing. The program takes into account crank position, throttle position, engine speed, engine temp, air temp, amount of oxygen in the exhaust and other variables. Based upon the sensor inputs it looks at the map to see how much spark advance to dial in, how much fuel to deliver (duration of injection pulse) and which cylinder to actuate at any given moment.
It is pretty impressive really, what they can do with modern technology.
Sometimes I have to pay people to do it almost the way I want it, but mostly it isn't worth the hassle so I do it myself.