A dragon digests its food, which then passes on into a second stomach where it is digested further. This turns left over food and acid into chemicals which are useful in producing fire.
Inside a dragon, near the second stomach, are several fire glands. Inside these glands there are different hydrocarbons, such as methane and octane. The glands are muscular structures and connected to the nervous system, so the dragon can control when it spits out fire.
The dragon also produces an oxidant agent and a strong high smelling, highly flammable fuel. So when the dragon opens up the glands it lets out hydrocarbons which mix with the oxidant agent and fuel, creating the beginnings of a fire.
By the time this fire has been let out of the mouth or nostrils, it has become a strong and powerful blast. There are many species of dragon that are able to control with great precision the shape, temperature and duration of the flame. This depends on how much control over fire glands a dragon has.