A LEGO gear can be fitted onto the shaft of most DC motors using heat-shrink tubing to fill the gap between the outside diameter of the motor shaft and the inside diameter of the LEGO gear.
Then, LEGO plates can be glued to the sides of the motor. This must be done so that the shaft and gear protrude at a legitimate LEGO spacing in order that the resulting assembly is compatible with the LEGO system.
See pages 51 through 54 of Chapter 1 of the 6.270 Robot Builder's Guide for detailed instructions.