It varies.
Handguns *typically* operate as blowback or delayed blowback, where the force of the gas on the back of the casing propels the slide backwards, which will then be returned forward by a spring. This forward/back motion includes other sub-steps that reset the trigger, eject the case, and pull a new round from the magazine.
Modern rifles typically use some variant of a gas-operated system, where the barrel has a hole drilled in it that leads to a piston, which then drives the action to go through the ejection and loading cycle.
Just about the only guns that are electrically driven are going to be cannons and gattling-style guns such as the [GAU-8](https://en.wikipedia.org/wiki/GAU-8_Avenger) or [M-134](https://en.wikipedia.org/wiki/Minigun).
This is a very broad overview, and many exceptions exist. If you want to learn the details about specific guns operating cycles, I highly recommend checking out https://www.forgottenweapons.com
Latest Answers