As a person is dying, the brain, just like the body, starts to sacrifice areas which are less critical to survival. As the brain begins to change and start to die, different parts become excited, and one of them is the visual system. That is where the people begin to see light.
There is a surge of (normal + entirely new) neuro-chemicals in the brain. The sudden release of neuro-chemicals may explain seeing the white light and having experiences ‘realer than real.’
Source: Palliative Care and the Science of What It Feels Like to Die - The Atlantic