When different ionic compounds are heated, electrons around the metal atom become excited and jump energy levels. When the electrons fall back to ground state they release light and heat energy. The amount of energy released determined the wavelength and color of the light released, resulting in different colored flames.
When a small amount of water is heated inside the can, steam is produced, filling the can. When the can is inverted into cold water, all of the steam condenses quickly causing the can to implode.