Are both expressions correct? If yes, do they have different meanings? It seems to me that I came across both of them in books, but I'm not sure.
They are both correct, but convey slightly different ideas.
I will sleep on a bed.
Implies that I will sleep on the piece of furniture I refer to as a bed. As opposed to sleeping on the couch or sleeping on the floor.
I will sleep in a bed
Specifically conveys the idea that I will be inside the bedclothes, typically underneath a blanket or duvet.
Both expressions are correct and can have different meanings. A bed usually is made of a surface for sleeping upon and a warm cover of some kind. The phrase on the bed would be taken to imply that something is placed upon both. Take this example:
I placed my luggage on the bed.
This would mean the luggage was placed upon the top of the upper surface of the bed and bedding.
The phrase in the bed would be taken that something is placed between the warm covering (or bed cover) and above the sleeping surface. It is placed inside the bed, as illustrated by this example:
The hot water bottle was placed in the bed.
If the hot water bottle was placed on the bed it would get cold and not warm the bed properly.
Thus if we use sleep as the action being performed, you can see that we can both sleep in the bed and sleep on the bed. For example:
He was too drunk to sleep in the bed and was found sleeping on it.
There is a very famous bed in event which can also be used to illustrate the difference:
This is John and Yoko on the bed:
This is John and Yoko in the bed:
Normally, one would say IN bed when they are going to go to sleep.
But before sleeping or after waking up, you can say:
I'm sleeping (in bed)
I'm in bed reading
I'm in bed watching the TV
I'm in bed having tea and breakfast
The OP's example
(to) sleep in the bed
the article ‘the’ suggests that the listener knows which bed is mentioned; or there is only one available bed in which to sleep. The preposition in tells us the person is under the covers, sandwiched between the mattress and the bedsheets.
(to) sleep on the bed
suggests the same as the above with the exception that the person is lying on top of the bed.