How were guns invented?

Originally, gunpowder was used in Chinese weapons called fire lances. They were bamboo tubes filled with powder which was lit to shoot out flames and sparks to burn and scare the enemy. Someone noticed that particles were ejected from these tubes and decided to try and eject a more solid projectile.

The rest is history.