Gunpowder (or gun powder) is a mix of chemical substances (charcoal, sulfur and saltpeter). It burns very quickly, and creates gases. Those gases use up more space than the gunpowder they come from, so they push outward. If the gunpowder is in a small space, the gasses will push on the walls of the space, building up pressure. In a gun, the pressure pushes against a bullet, causing it to fly out at high speeds. The pressure is not high enough, though, to destroy the gun barrel.
Gunpowder was probably invented by the Chinese, the first references of black powder, which is a form of gun powder date to the 9th century, when Roger Bacon described the formula of black powder. According to legend, Chinese alchemists were looking for a formula to create the elixir of life, or the mythical potion that causes whoever drinks it to become immortal, when they accidentally created gunpowder. Because the powder was highly flammable, or burned very easily, they decided to call it "fire medicine" (Simplified Chinese: 火药 / Traditional Chinese: 火藥). Shortly after the discovery, the Chinese weaponized the substance and throughout the centuries they would make many weapons using gunpowder, including rockets, bombs, flamethrowers, and land mines before making cannons and guns.