Not to get too technical, but here is how it works on an Otto cycle engine. (And why octane matters.)
Intake stroke: Piston generally is going down, this creates low pressure, and the open intake valve allows air/fuel in the manifold to be drawn into the chamber/cyc. (Or in our case, sometimes forced into place by boost.)
Compression stroke: Piston generally is going up now, compressing the air and fuel mixture. This is where it can get dicey. If you compress the mixture too much, it can light off like a diesel engine does, and since this is not a diesel motor, that is a bad thing. This "Detonation" event happens before, or during the normal spark induced ignition of the air fuel mixture. More fuel tends to cool the process, and that's why running lean causes problems. Not enough cooling, so it lights off too soon.
Now comes the power stroke: In the perfect world, you light off the mixture just before Top Dead Center, while the piston is still going up slightly. This allows the fuel to be burned completely by the bottem of the stroke, and adds to the power you feel. (This is spark advance.) In a detonation event, the mix is lit off either too soon, or in multiple places in the combustion chamber. These events disturb the gas boundry layer that protects your pistons/head from the burning gas. (Which is at a temp that melts these metals.) Suddenly your piston and head metal temps go very high, very fast. These hot spots are now capable of lighting off fuel as it comes in to the chamber, even uncompressed. (Worst case)
Last is the exhaust stroke, pushing it out the exaust valve as the piston goes up, and the whole cycle starts again. (4 stroke engine.)
Worse case, the metal is still hot from the detonation, and now the incoming air and fuel is lit off as the piston is going down. This burning fuel is then compressed by the engine, and with the inertia of the assembly, your pressures go insanely high. This further heats the metals, and your normally solid pistons/head and other parts suddenly become silly putty. Or things bend. Or they break. Or a combination of all the above happens. This is called pre-ignition, and is generally the result of extended detonation events. (Extended at 6500rpm is like a second or two.)
What octane "does" is rate the fuels ability to resist being lit off by heat alone. It is literally harder to get burning that lower octane. The higher the number, the more heat it will take to start the fire. (The plasma of a spark is very hot, and will light off even the highest octane fuels.)
The cool part is, with higher octane fuels, you can run more pressure, or compress more air, and that means you can burn more gas, and that adds up to more power for each stroke. (In theory, and in practice.) Your engine is nothing more than a very expensive air pump, fueled by burning gas to pump more air. (So you can burn more fuel... ) The turbo is a great extension of this, pushing air into the motor at rates beyond what simple normally asperated engines can achieve. (And add more fuel.)
Key is, you control the fuel and spark, or detonation controlls you.
Pet pevee of mine is hearing automotive teachers call anything associated with normal engine opperation an "explosion." It is a controlled burn of the fuel. Explosions are uncontrolled, and cause damage. This is an engine, not some demolition experiment. You burn fuel, you don't explode it. (Unless your trying for a large explosion, then I reccomend C4, and possibly nukes if you like that sort of thing.) Even those are actually controlled burns if you want to get technical, the burn rate is just measured in (Tens of) thousands of feet per second, so it "explodes" outward in every direction.