A German proverb published in 1617 states "Wer sein Bett beschissen hat, muss sich darein legen" ("He who has beshit his bed must lay in it"). A similar Italian proverb, "Chi fa il letto, se lo dorme" ("He who makes his bed will sleep in it") was recorded in 1536.
The phrase entered the English language sometime in the 1600s, and it has been used in various forms since then. It has been used literally to refer to the sheets and blankets on a bed, but it is more often used figuratively to refer to the things a person does that can affect them positively or negatively.
In English, the earliest citation recorded in the Oxford English Dictionary is from the year 1640 in Robert Davenport's comedy The City-Night-Cap:
You must lie in your own bed, how foul soever you have made it.
A variant from 1686 goes as:
If thou mak'st thy bed ill, Lie down and sleep ill.