To work late into the night. Originally this was by the light of an oil lamp or candle. More recently, the phrase is used figuratively, alluding back its use before electric lighting.
The English author Francis Quarles wrote in
Wee spend our mid-day sweat, or mid-night oyle;
Wee tyre the night in thought; the day in toyle.
At that time there was a verb for working late by candlelight - elucubrate. Henry Cockeram defined that in his The English dictionarie, or an interpreter of hard English words, 1623:
"Elucubrate, to doe a thing by candlelight."
Clearly, we no longer have much call for that word and it has fallen out of use. Although it is probably some years since anyone needed to do it in reality the phrase 'burning the midnight oil' is still in everyday use. Porter Wagoner and Dolly Parton released an album called The Right Combination/Burning the Midnight Oil, in 1972.