I just visited Versailles and learned some things about Louis XIV one of which is that The Sun King had 48~ wig makers on staff. I have many follow up questions.
I have to start with: I don't know, exactly. I'm not entirely sure whether anyone knows. The unfortunate thing about studying historical material culture is that people in the past often failed to record very commonplace practices because they never felt the need to. Imagine being a person in the future who only knows doors with magnetic key cards and automatic locks, trying to figure out how we today decide when to lock doors and how keys physically work. The Duc de Saint-Simon noted in his memoir of life at Louis XIV's court in the early eighteenth century that the king did have a "little short wig" that he wore when he "appeared" in bed, but that likely means that he had it on by the time he performed the lever rising ceremony - not necessarily that he wore it the entire time he was in bed.
One thing we do know, however, is that men didn't typically wear wigs at all times. When relaxing or going to bed, men customarily removed them and replaced them with embroidered caps such as this one owned by the Cooper Hewitt. These caps actually predated the fashion of wigs - they were worn as replacements for hats during the sixteenth century as well.
White linen cap embroidered with black and silver acorns, 1550-1600
White linen cap embroidered with metallic and black floral vines, 16th century
White linen cap embroidered with white linen, ca. 1550
White linen cap embroidered with black and silver leafy vines, 1580-1620
White linen cap embroidered with silver and colored trees and flowers, 1600-1624
White linen cap embroidered with silver, gold, colored silk, and spangles, 1600-1624
In terms of both materials and embroidery designs, these are actually quite similar to women's coifs and informal jackets of the period, interestingly enough.
By the end of the century, when men tended to wear them as replacements for wigs rather than hats, it was more common for men's caps to be made of a colored fabric and embroidered with larger motifs.
Yellow silk cap embroidered with flowers, 1675-1725
Yellow and blue silk cap embroidered with flowers, 1680-1720
These caps were usually worn while men were partially dressed, with their doublets or coats off and with a dressing gown (called at the time a morning gown, wrapping gown, banyan, or other terms) on. Despite their extremely basic construction during this period - made in the shape of a T, typically with no fastenings, although they could be belted with a sash - they were symbols of wealth and leisure, usually made out of silk or imported cotton calico, which at the time was somewhat exotic. In the memoirs of the Duc de Saint-Simon, it's noted that Louis XIV had dressing gowns, and that he even wore them to dine in front of courtiers toward the very end of his life.
As I noted in an earlier answer to a question about how people had sex in the eighteenth and nineteenth centuries,
But how often would married couples go from zero to sex? While premarital sex happened in all kinds of places depending on when couples could get alone time, marital activities seem to have generally been done in the marital bed. It's hard to find direct evidence of this, at least in the sources I have to hand, but the fact that women who wanted to avoid their husband asserting his conjugal rights (i.e. marital rape) often achieved this by avoiding the bed - by having their own bedroom, by staying up late with work, by sleeping with the children - implies that most sexual activity would be expected to happen there. (Separate bedchambers were the norm for the upper and upper-middle classes, with the wealthiest even having attached dressing rooms. I can't say exactly how far down the social scale they continued, but the working classes definitely shared beds and bedrooms.) In these cases, women and men would have already undressed and be in nightclothes, so the point is rather moot.
The majority of the sex that Louis XIV had was outside of his marriage, but the point likely still stands. Television spices up sexuality in order to titillate the viewers, but there's no reason to think that he typically went directly from full dress to sex. He had no need to be secretive, and could arrange to visit his mistresses in their official apartments or have them come to his own at his leisure, when he was in relaxed dress, probably after the coucher ceremony where he was put to bed. In which case, the day's wig would most likely have been on its stand and a cap on his head, although it's possible that he would have tried to wear his short bed-wig (though it seems unlikely, since there would have been nothing to hold it on during vigorous movement).
The short answer to this question is, possibly wig-off, but it's a little more complex than that.
Louis XIV wore his own, natural hair long as a young man, but allegedly made the move to wigs at some point in his late 20s or early 30s, to cover baldness. The exact date is disputed - I've found two secondary sources, one of which states c. 1660, the other 1672. However, we can be certain that Louis is wearing wigs from about the age of 30 onwards, if not earlier.
This portrait by Charles LeBrun, dated c.1661 when Louis would have been 23, is ambiguous - the hair is very full, but the style is meant to appear natural. Another portrait of that period, in pastel, shows the young king with shorter hair, but again with a natural appearance. For contrast, this later portrait, probably painted after 1671, shows a more obviously 'wiggy' appearance, in terms of the volume and shape of the hairstyle. From this period onwards, Louis' wigs get more and more stylised and artificial in shape, with the crown of the hair puffed higher and higher, so by the time you get to the 1680s and 90s, you're dealing with this.
Anyway, let's assume we're dealing with Louis in a wiggy period of his life - and, just for info, it would have been referred to as a perruque, which makes its way into English as 'periwig', from which: wig. Although you do see occasional English references to 'perukes' being worn in the 17th and 18th centuries.
The full wig of this period was not an easy thing to wear. Aside from being large and heavy, there was no apparatus to ensure that it was anchored to the head, particularly as the natural hair underneath was normally worn cropped short. Of course, a man of Louis' wealth and prestige could call upon the expertise of the very best perruquiers, or wigmakers, and would have enjoyed a bespoke service that included fitting the wigs to the dimensions of his head. Nonetheless, they were unwieldy. When being fitted for his first wig in 1663, the London diarist Samuel Pepys records his dislike of the fashion, on account of the 'trouble' it entails. He also records several incidents in which gentlemen of the court had their wigs pulled off in arguments.
A wig was a marker of status and prestige, and by the late 17th century it was an essential part of men's formal dress. By the mid-18th century, most men bar the poorest, and a few holdouts who preferred their own hair, wore wigs, at least for formal occasions if not every day. The deliberate artificiality of Louis XIV's court at Versailles, and the importance of dress and deportment in demarcating social hierarchy, meant that the King was no more likely to go without his wig in public, than without his shirt or shoes. This secondary source claims that only the King's barber was permitted to see him without a wig on - despite the fact that his daily dressing and undressing (the lever and coucher) were ritual events attended by favoured courtiers.
To work out whether this extreme degree of formality extended to the bedroom, we need a sense of how 17th and 18th century Europeans approached heterosexual sex. Vic Gatrell observes, in City of Laughter, that if satirical and erotic prints of the period are remotely accurate, people were far more likely to remain at least partially dressed during sex, and to do it sitting or standing up, rather than horizontal. Allowing for some artistic license, there are visual clues which suggest that the wig would have been worn during sex. A slightly later, but still useful, example, is William Hogarth's Before and After. This pair of images show the scene leading up to, and after, an ambiguous sexual encounter in which the question of consent is not entirely clear. Either way, the male protagonist is still wearing his wig in the second, 'after' image.
There were precedents for men appearing without their wig in a public setting, as those with the means to have their portraits painted could choose to appear in a soft cap or turban covering the head, or even with the hair short and natural, usually worn with a robe or dressing gown. However, this mode signified informality and scholarship, and would not have been appropriate for the image of kingly grandeur which Louis wished to project. It's something you find in Sir Godfrey Kneller's portraits of people like Sir Isaac Newton and the philosopher John Locke - not a royal image.
So, without being able to offer you a definitive answer, it is at least plausible that Louis XIV kept his wig on for at least some of his sexual encounters. It's fun to speculate that, with his mistresses of many years' standing - Madame de Montespan, for example - he might have permitted a greater degree of intimacy, but we have no proof either way. I'd like to think that he gave himself a break from the wig while in bed, because all the evidence points to there being a very real risk of it falling off in the middle of things, which honestly sounds like a passion-killer, even from a 21st century perspective.
Wasn't the main reason they wore wigs was to hide syphilis?