What causes KNO3 to decompose into KNO2 and Oxygen in gunpowder?
KNO3 (Potassium Nitrate) is a molecule that is used with pyrotechnics and is typically mixed with Carbon and Sulfur as fuels. 

When Potassium Nitrate is thermally decomposed it forms Potassium Nitrite and Oxygen.
$$\ce{2KNO3 -&gt; 2KNO2 + O2}$$

Potassium Nitrate is known as an oxidizer for this reason (it provides Oxygen to the reaction). This free Oxygen then reacts with the fuel sources in combustion to create an exothermic reaction. Carbon is typically the main fuel source and Sulfur is added to reduce the ignition temperature required to begin combustion of the Carbon-based fuel.

If you had a mixture of Potassium Nitrate and Sucrose (table sugar), you would see something like this:
$$\ce{5C12H22O11 + 48KNO3 -&gt; 24K2CO3 + 36CO2 + 55H2O +24N2}$$

Where you can see that you have a combustion of the two reactants (after the Potassium Nitrate thermally decomposes, it provides the necessary Oxygen to begin combustion) resulting in the products of combustion, Carbon Dioxide and water, along with the byproducts of Nitrogen gas and Potassium Carbonate.