As I parse it, "The hell?" seems to be a "shortening" of "What the hell" which is a shortening of "What in the hell?" or "What in hell", Which could be a shorter version of "what the hell is that?" which could be expanded to "What thing that is in Hell/heck (is that object/is happening/am I witnessing)?" So if you say "What the heck?", "the heck" could be part of a prepositional phrase: "What (thing [in) the heck] is that?" It's strange, though, that "the heck" or "the hell" are specified, as "a hell" isn't normally talked about, and "Hell" is used as a name of a place, rather than a type of place.
Now, you could take "What the heck?" literally, and you'd be asking Which version of "the heck" that was, or something like that. Now, in "what in hell", we're asking "What (thing (that exists)) in hell (is (something/that)/is going on/did I just witness)?".
It's interesting to me (although I couldn't say how on topic) to note that most objects by the utterer are usually not considered to actually be in hell, although in "What on Earth?", most objects actually are considered to be "on Earth", and astute/snarky/pedantic/literalistic persons will note as such when someone says "What on Earth?" about something which is not actually on Earth. (Example: "What on Earth is a dwarf planet?" "Technically, there are no dwarf planets on Earth.")