An explosion in a confined space with only one outlet - the gun barrel. The gas inside the containing area expands at a tremendous rate creating enormous pressure on the walls of the container - the breech area of the gun and the inside of the gun barrel. But blocking the expansion is the bullet, or shell or...in the olden days, the cannon ball. The breech is locked, the barrel is strong, but the bullet is only fixed fairly loosely into the shell case. So, 'voila!' the bullet is shot out the end by the expanding gas.
The barrel is straight. This give the direction and the accuracy. Gun barrels are, of course, quite technical but, basicially, they are either 'smooth bore" as with a shot gun, or have spiralled rifling in the barrel. I won't go further into that here.
In the old days there was no breach. muskets and early rifles, pistols and their big brothers the cannon and canonade, were loaded by pushing a bag of black gun powder into the muzzle with a ramrod. This would be followed by a wad of two to help build up pressure when the gun powder was ignited- generally by touching a burning piece of hemp or some such into the power which lay just beneath a breach hole. So it would be gunpowder, wad, and a cannon ball in last. All rammed home down that barrel with a long pole or ramrod.
With the invention of the bullet contained at the front end of a shell case (or bullet case) the expansive powder (now white) could be enclosed in the shell casing. This meant that a man could carry bullets rather than bags of gunpowder and shot. The breech loader came in around that time, also. First it was the single shot weapon, but repeaters quickly followed.
Later it was realized that the expanding gases from a bullet leaving could also be used to place the next bullet ready for firing, hence the sem-automatic and, later the machine gun.
But no matter how far the sophistication went, a gun works on the same old laws: expansion of hot gases and only one way for the projectile to come out.
Hope that helps.