Fire is caused by the three elements i.e. fuel, oxygen, and heat. If one of the three elements is removed fire will stop. Carbon dioxide removes one element, i.e oxygen.
CO2, being heavier than oxygen, covers the fire like a blanket. Since the contact between the fuel and oxygen is cut off, the fire is controlled. Therefore, CO2 is used in fire extinguishers.