The following images explain how a Lego 4-speed gearbox works.
The brown numbers indicate how many teeth each cog has. The blue numbers indicate which gear the image explains. The green marks on cogs indicate which cogs are rotating to transfer the drive (some additional cogs in the image might also be rotating as idle).
For example, when 3rd gear is selected, the red holder adjacent to 3rd gear clutch (as marked by 3 in the image) moves and attaches to it. Then the associated cogs marked green starts to rotate and finally transfer the drive to wheels.
For example, when 3rd gear is selected, the red holder adjacent to 3rd gear clutch (as marked by 3 in the image) moves and attaches to it. Then the associated cogs marked green starts to rotate and finally transfer the drive to wheels.
Please click on each image to view larger size.
If you want to model it, you can do this using either studded or studless beams. When using studded beams, there needs to be 2-plate depth separating upper and lower beams (to hold the axles). Please also note that inside the driving rings, there must be axle joints (as shown in the instruction manual for 8880 for reference).
I can confirm that I have actually modelled this mechanism and it works as shown.
If you own any Lego Technic sets like 8880, 8448 or 8466 then you already know how gearbox works!