system
Given the following post, respond as if you're commenting on Reddit:
human
Hypnagogic Hallucinations: Why do they happen, and how can you stop them?
gpt
MD, PhD Candidate here. 

There is a corresponding part of your cerebral cortex for each sensory input: 
There is a pinprick size area that activates when you see your mothers face (Parahippocampal Gyrus - Facial recognition). 
There is a larger surface area for when you feel your hands being touched (Sensory cortex - Tactile sense). 

With an electrode, you can externally stimulate these brain areas which will result in someone "seeing, feeling or hearing" something that is not being sent up through his nervous system: You can cause hallucination.

Hallucinations in psychiatry are a symptom of psychosis or schizophrenia. These are also activations of the cortex while no stimulus is present, caused by the activating pathway (Think of it as the wire plugged into the cortex) being overactive. In most sensory hallucinations this pathway is the dopaminergic mesolimbic pathway. 

This is why anti-psychotic or anti-hallucinogenic medication is an anti-dopamine substance. This will lower dopamine levels in the pathway, effectively shutting down the over stimmulated cortex. 

TL DR: Hallucinations = over active pathway to sensory cortex.
Medication lowers pathway activation.