What happens to the brain when we sleep?

Contrary to popular (unscientific) belief, the brain does not slow down or rest when we go to sleep. The same regions of the brain that are active when you are awake are active when you sleep. What disappears is the inter-connectivity between the regions of the brain. It's the inter-connectivity that defines our conscious, awake state. Without it, we are running purely on automatic response.

Imagine that the brain is a colony of ants. Each ant has a particular job which it carries out by interacting with the rest of its sisters. In this way, an ant colony is like a single organism with multiple cells--each cell with its own function. Much like the bodies of larger multicellular organisms with their differentiated cells structures. Ants communicate and navigate by specially "designed" chemical compounds which are left behind as program markers or commands.

What happens to an ant when you remove all the chemical markers it was following? It enters a state of indecisiveness where the ant begins to wonder aimlessly in search of a command. The same thing happens to the brain when you are asleep and the different regions have lost the ability to communicate with each other. Each region that actively contributes to the conscious mind becomes a wondering ant, separated from the colony.

Eventually, the ant, out of frustration will start to carry out tasks that are familiar to her even though she has received no instruction to do so. For instance, she will start to pick up and carry food even though she has no idea where to carry it to, having lost the chemical markers that led back to the colony. This must be what happens when we dream. The different regions of the brain get frustrated from not receiving signals from the other regions of the brain and begins to invent scenarios that have no external purpose. It creates "busy-work." It starts to work out dilemmas and situations that have no actual source. This busy-work manifests as disjointed thoughts that are recorded in isolated regions of the brain. In other words, dreams.

Dreams are not typically recorded by the brain. They are discarded as quickly as they are made. Unless a person wakes up while the disjointed dream-thoughts are still active. At that point, the inter-connectivity becomes active once more and with it, active memory is restored throughout the regions. Since memory is not centralized, it doesn't matter where the thoughts were created. So long as inter-connectivity is reestablished, dreams stored in any part of the brain can be integrated into the awakened mind. Which is why the dreams that we remember most vividly are the ones that we were having at the time we wake up.

Do dreams have purpose? And if they do, what kind of purpose? Is Freud right when he claims that dreams are wishes which are in unconsciousness?

Doubtful since there is no purposeful cognition behind dreams. They are just clouds in the sky. You see the shapes you want to see. If you want to believe that dreams are unconscious thoughts, then you will force yourself to create delusion-unreasonably excuses.

Is it possible to learn something while you are sleeping?

Some people use those tapes that you play in your ear while you are sleeping to try to learn languages or stop smoking by subliminal suggestion. I don’t know of they actually work. Hypnopaedia is old concept, Hinduism claim that whole Vedas were learned by pupils in that way.

Neurologically, the brain is as active when you are asleep as it is when you are awake. The same processes in the same regions continue to fire off data at all times. What you lose when you are asleep is the inter-connectivity. The different regions of the brain lose the ability to communicate with one another. So perhaps whether or not you can learn anything in your sleep depends on if your consciousness can navigate all that chaos. It would be like trying to find a particular country on a map when all it shows is the geography and no border lines or city markers.