You are actually spot on. In the second case, rooms is the subject (it does the having), while in the first one it is the object (it is being had) and the subject is apartment. That is all there is to it.
And this is not something limited to questions asking for "how many". Compare for yourself:
Who killed Sarah?
Who did Sarah kill?
What changed you?
What did you change?
What kind of person bites a puppy?
What kind of person does a puppy bite?
In one case you're asking for the subject, in the other for the object.
As one last illustration, let's go back to your original examples and note how each of them actually does come in both variations — it's just that one of the variations happens to be nonsensical.
How many rooms does your apartment have?
= I am asking for the number of rooms that are in your apartment. Makes sense.
How many room have your apartment?
= I am asking for the number of rooms that own your apartment. Nonsensical as of course your apartment is owned by a person, not by rooms. But other than not making sense, it's a perfectly grammatical structure. In fact we only know that it makes no sense precisely because we have successfully parsed the sentence and understood what it is asking for.
How many rooms have carpeting?
= I am asking for the number of rooms with carpeting in them. Makes sense.
How many rooms does carpeting have?
= I am asking for the number of rooms owned by carpeting. Nonsensical as of course carpeting does not have rooms. But note how if you replace carpeting with something that can have rooms, the exact same question is immediately fine, e.g. "how many rooms does this house have?"
Likewise, you can leave the carpeting in peace and replace the rooms with something that makes sense, and once again the question is perfectly fine: "How many colors does the carpeting come in", or "how much smell does the carpeting cause", or "how much additional work does the carpeting mean", and so on.
So, to sum it all up:
- One question form is for asking about the subject of the action at hand, the other for asking about the object. This part is about grammar.
- Depending on what the action at hand is, sometimes only one of the questions makes sense, and sometimes both. This part is about meaning.