Why are dreams sometimes so Vivid?

Last night I dreamed that Bob came home.  I heard his foot steps in the hall, and he came into the bedroom and stood beside the bed for a minute.  Then he lay down beside me.  His foot was healed, and he was like he was many years ago.  Then Eboni came and nudged me, wanting to go outside.  I got up and let her out.  When I got back into bed, she was there again; and once more.  I said to myself "how many times am I going to have to let you out?"
The dream was so "real" that as I was waking up I looked to see if he was in his bed.