As sunlight enters our atmosphere, it bends slightly. This is due to refraction, the same thing that makes a pencil look slightly askew when you stick it half way into a glass of water. The atmospheric bending of light rays is greatest when the sun is right along the horizon, at sunrise or sunset.
How does bending sunlight create a green flash? When sunlight is bent by refraction, it separates into different colors, just like light through a prism. While a prism can make a dramatic rainbow, atmospheric refraction isn't that strong. It doesn't turn the sun into a rainbow, but it makes the lowest part of the sun seem a bit more red, and the top of the sun seem greenish blue. The effect is so subtle, you'd never notice it.
Except, of course, at sunset or sunrise. For a split second, when most of the sun's globe is below the horizon, the only part showing is that blue-green sliver along the top. Blue light is scattered by the atmosphere, so what's left is a brief flash of green, the only illumination in the sky!
Please remember, this happens immediately after a sunset. Never look directly at the setting sun.