This is an atmospheric phenomenon caused by sunlight being dispersed into its different colors, and then refracted to varying degrees by the atmosphere. I am not an expert in such matters, but I presume that it is caused when much of the light wavelengths shorter than the green color we see in a rainbow are scattered out of the sunlight from the sun close to the horizon. This leaves only the wavelengths of green, yellow orange and red which combine to give a predominantly green color. Quite a lot of diffraction is needed to accomplish this feat and this meanns that a very large patch length through the atmosphere is needed. It would occur only in the last few sesonds before the sun actually sets.
For more information, you might check out an article in Sky and Telescope magazine published on September 1992 for discussions about star colors and the human eye. There is also an article in the February 1992 issue on page 200 about the Green Flash itself, which presents several other explanations for this phenomenon and weighs their pros and cons.