It’s weird how I’ll see a dream and really ponder over it right after waking only for it to be completely out of my memory shortly after.
You don’t form longterm memories while asleep; you lose the last few minutes before you drop off, as well. Short-term memories are held, but they don’t get stored.
And the same applies while you’re dreaming - nothing’s getting recorded. You can pull it out of short-term right after you wake up, but that fades right out.
When you do remember dreams, it’s because you remember remembering them while you were awake. If it was vivid enough to go over hard enough, then the second-hand memory gets stored.
Basically when you sleep only RAM works and no ROM storage is active, when you wake up ROM comes online, if you don’t pull data from RAM fast, it will be overwritten by other things and you lose it forever.
As with most brain questions, nobody knows exactly.
One theory for the purpose of it, is to prevent us from confusing things we dreamed about with things that actually happened.
Edit: Lol, this always gets downvoted, but it’s the truth. We know nothing about the brain; almost all the studies that get reported on are basically this.