As with all dissociative disorders, this behavior cannot be explained by drugs or other medical conditions (like seizures).
Most sufferers of DID will have on average ten personalities, though it can range from two to hundreds of separate identities.
Nobody can agree on whether it exists, what causes it, at what age it typically can be diagnosed, and so forth.
The fact that it is incredibly rare doesn't help.
Often, some memories will be available to the alters that the primary will not be able to recall. The official ruling so far is that DID is caused by extreme trauma, normally during childhood.
The abuse is so severe that it prevents formation of a coherent identity, resulting in memory fragmentation.
The individual cannot handle the memories of the abuse, so alternate personalities are formed to "store" these memories and handle other life aspects.
Essentially, DID forms as a type of coping mechanism to help the sufferer deal with what they experienced as a child.
Truthfully, we really don't know nearly as much about it as we do most other disorders, which is perhaps why it's such a ripe trope in fiction.
Formerly known as Multiple Personality Disorder, DID is diagnosed in individuals where they present more than a single distinct identity or personality, and control of the body is shared amongst the host and these "alters." Each personality will generally show a varying degree of memory lapse that can't be explained by normal human forgetfulness.