Natural gas is mostly composed of methane (CH4). The engine itself works largely the same way that a traditional gasoline or diesel engine works, just with a different fuel source for combustion. The actual chemical reaction is:
CH4 + 2O2 --> CO2 + 2H2O
Basically the methane is mixed with air (containing the oxygen) and pumped into the cylinder where the spark plug ignites the mixture and causes the piston to pump up and down. The action of the pistons ultimately rotates the transaxle and transmission system that in turn causes an axle to rotate and turn the wheels.
Natural gas engines have positives and negatives compared to gasoline. You still produce CO2 like with gasoline, but methane is a cleaner burning fuel, burning more efficiently than gasoline and producing fewer byproducts like carbon monoxide. But gasoline is a liquid and as such has much more energy density (kilojoules per unit volume) than a gas and is easily transported and stored. Natural gas is a gas and is harder to transport and store on a moving vehicle. Plus there is a safety factor. When a gasoline vehicle gets in a wreck, there is very rarely any fire or explosion (that only happens in movies). A tank of natural gas is highly pressurized, however, and if the tank ruptures in an accident explosive gas will leak into the entire area, a serious safety hazard.