Why is it grammatically incorrect to say the following sentence:
Du musst in Bett bleiben.
when I'm trying to say
You have to stay in bed.
Why must one use im instead of in in this situation?
Both English and German (and presumably many other languages) have a number of "fixed phrases" where grammar doesn't always follow the usual rules and meaning isn't always what you might expect. When you translate these from one language to another all sorts of mysterious anomalies may occur. The English "in bed" is one such fixed phrase. Normally "bed" is a countable noun, which means it must be preceded by an article or other determiner; you must say "I have a bed", not "I have bed". But "in bed" breaks this rule, and it's broken often enough that no one even notices, in other words it's a phrase where grammar doesn't follow the usual rules. We can't expect German or any other language to have exactly the same exception. Note that the actual meaning of the phrase is "in a/one's bed"; if you tell someone "You have to stay in bed," then you don't really care which bed since the point is that they remain warm, rested and horizontal as much as possible.
Similarly, the German im Bett is also a fixed phrase; when you use the definite article in German then both you and the listener know which specific item is being referred to. When you say Du must im Bett bleiben, again, you don't have a specific bed in mind. The German version is, at least, grammatically possible, but this time the meaning isn't what you would expect. So due to the fact that both the English and German use fixed phrases for the situation, and English and German don't agree on how the fixed phrases should be, um, phrased, you get this anomaly when you translate from one language to the other.
To show there is no widely applicable logic being used here, consider the similar phrase "You must stay at home." Again, "home" is a countable noun and normally requires an article or other determiner, but "at home" is an exception. Not only that, but the preposition "at" doesn't have its usual meaning here, what is really meant is something like "You must stay in your home."
The German equivalent is Du musst zu Hause bleiben. If we were to follow the same logic as "in bed", then the German version would be Du musst zum Haus bleiben, which I'm sure sounds very odd to German speakers. The German uses the preposition zu instead of in or bei as you might expect given the meaning. The expected determiner is not there (coincidentally, as in English). In addition, the outdated -e ending has been added to the dative.
In general, it's best to be aware of these fixed phrases and memorize their meanings as if they were additional vocabulary words. Sometimes it's only the English side that's "at fault", in which case you have to recognize that fact and not assume that German is being illogical. Part of the experience when you learn a new language is that you get a deeper understanding of your own language. I have well over a hundred such fixed phrases listed in my notes. Wiktionary lists over 2000 but, in my experience, Wiktionary's categories usually have a low signal to noise ratio.
A bed is a concrete object. In German specific objects are used with an article (or another determiner). In the following example, das Bett, in conjunction with the preposition in, is used in dative case:
Du must im Bett bleiben.
(You need to stay in bed.)
Notice that im is short for in dem or in einem. There are situations where it doesn't matter whether the short or the full form is used; but in this example, the short form is more idiomatic. In fact, the variant
Du must in dem Bett bleiben.
(You need to stay in this bed.)
would be also correct, but since most people would interpret dem as a demonstrative pronoun, this variant puts much more emphasis on a very specific bed that the person refers to.
The situation is different with abstract things, which, like in English, are usually used without an article. For example:
Wir müssen in Verbindung bleiben.
(We need to stay in touch.)
Here, we do not refer to a specific hard-wired connection or touch, but to an abstract connection between people.