When trigger is pressed, a hammer releases and strikes firing pin. The firing pin punctures the bullet primer, which contains a powder which explodes to ignite the gun powder.
Expanding gases force the gas piston backwards, which forces the bolt carrier back, ejecting casing.
The bolt pulls a new round from the magazine and inserts into the barrel. Bolt carrier also resets hammer into starting position.
A sear holds the hammer in place till the bolt carrier returns to its position.
This is a basic visual of how it works.