There is the theory that at the last moments of a person's life, the moment before their brain ceases to function, which is as their heart weakness or stops, they secrete hormones that put them in a state of euphoria, similar to when people start to die from freezing temperatures and feel a warm cozy feeling. In these euphoria time seems to elongate and people may enter a dream-like state. Thus some people who "died" and came back to life may say they visited heaven, when in actuality they entered a sense of euphoria to prevent the horrifying feeling of a heart attack.
The theory in itself lacks a lot of evidence, and it's not something that be tested very easily, as it may require putting people in dead states for testing, something that is next to impossible to approve by the health scientific society.