Two things determine the octane rating an engine needs and/or is optimized for:
1: compression ratio
2: ignition timing
Other things can affect it too (e.g. air to fuel ratios) but most emissions laws make these non-players: to meet emissions, the engine computer (ECU) can't command an over-rich air-fuel ratios to compensate for lower octane fuel any more.
Higher compression ratio = more thermodynamic efficiency (more HP per gallon or more MPG for a given HP level) but more tendency to knock or pre-ignite. Pre-ignition is what limits compression levels in engines today. Higher octane means the fuel is more resistant to self-ignition (igniting due to compression pressures & temperatures) which delays knock/pre-ignition so higher compression engines tend to require higher octane fuels. At some point, if octane is low enough, the compression in the cylinders will ignite the air+fuel mix without any spark - like a diesel engine. For a gas engine, this is very bad because "compression ignition" is a violent burn - more like an explosion.
Ignition timing exists because it takes a finite amount of time (albeit short) for the
spark plug's spark to spread to the whole combustion chamber and get the whole air+fuel mix burning. Ideally the "burn" happens just as the piston is about to go downwards, on its power stroke, so the burn pushes the piston down as much as possible. If the burn is late, the piston has already started down so some chance to make power is lost. If the burn is too early, the piston is still on its way up and the burn actually tries to push it backwards --> subtracting power. This is extremely high stresses on the engine by the way - busted pistons, bent/busted connecting rods, etc. are the usual results from excessive early combustion. The whole point of ignition timing is to advance the spark just enough so that the burn peaks just as the piston is ready to move down. The spark therefor has to happen as the piston is still moving up typically - and the higher the engine RPMs, the "earlier" in the rotation cycle the spark needs to be to have the same physical amount of time to spread to the chamber. The quantity of air+fuel in the chamber affects the amount of ignition timing advance too - more air+fuel (i.e. you've got the throttle pedal mashed to the floor) is denser in the chamber so the burn spreads faster and less advance is needed. Thus ignition advance is a function of engine RPMs plus amount of air+fuel in the chambers.
The problem is... the ideal point to fire the spark also varies with how ready the fuel set to burn. Low octane fuels burn easier... so the spark can't be quite as early as it needs to be with higher octane fuels. If the spark is too early for the octane, the burn starts and then "flashes" rapidly through the rest of the air+fuel mix. In mild cases you get the "pinging" sounds; in severe cases the "burn" is more like an explosion ==> smashes parts rather than increasing pressure at a more controlled rate that pushes the piston down.
Modern computer controlled engines have one or more "knock sensors" attached to the engine block; they're basically small microphones. They "listen" for certain tell-tale frequencies in the block - when found, that's early knock/detonation/pinging. The computer reduces the ignition timing advance until this goes away. The computer constantly tries pushing its luck; re-advancing the timing until the knock sensors detect problems. When you give the car higher octane fuel, the computer will slowly detect "hey, I can get away with more timing advance" and HP and MPG can improve.
On older cars, with mechanical distributors that use centrifugal weights and vacuum advance mechanisms to control timing... this doesn't happen. Those mechanical setups can't "learn" or adapt to different octane ratings. So they're generally set conservative so one tank of bad gas won't hurt the engine... though that setting doesn't take full advantage of any good gas either.
Cars designed to run on higher octane fuel - those with higher compression ratios - suffer faster with lower octane fuels... compared to what MPG or HP gains an engine designed for regular octane sees when given the high octane stuff. That high compression engine has to really reduce ignition timing to compensate for lower octane fuels and thus keep the compression from igniting the air+fuel mix... this hurts MPG and HP. A low compression engine can advance timing and get big benefits with high-octane fuel.
Knock/pre-ignition is what limits the compression ratio possible in engines. With modern fuel delivery techniques, better shaping of the combustion chamber shapes (to avoid local "hot spots" that want to ignite faster), reverse-flow cooling systems (radiator water goes from the radiator to the cyl head first, then the block, in modern engines so the heads are cooler and it's the heads that form the combustion chamber), compression ratios have been increasing over the last few years. Direct fuel injection helps a ton: if there is no gas in the chambers during the piston's compression stroke, there's no chance of knock/pre-ignition. Direct injection sprays the fuel into the cylinders once the piston is ready for the burn. And this sprayed-in fuel happens to help cool the air compressed in the chamber reducing the chances of self-ignition. Ergo more compression ratio can be built into the engine in the first place.
mike c.