Step 1: Analyze each option for grammatical correctness.
(1) I asked them what they were doing.
This sentence follows the correct structure for reported speech. "what they were doing" is a subordinate clause reporting a question, and the subject ("they") precedes the verb ("were"). The tense is also correctly shifted to the past continuous ("were doing") because the main verb ("asked") is in the past tense.
(2) I asked them what were they doing.
This sentence has an incorrect word order in the reported question. In a subordinate clause reporting a question, the subject should come before the verb. It should be "what they were doing," not "what were they doing."
(3) I asked them what have they been doing.
This sentence has an incorrect tense in the reported question. Since the main verb ("asked") is in the past tense, the verb in the reported clause should also be in a past tense form. "have they been doing" is present perfect continuous. The correct form would be "what they had been doing."
(4) I asked to them what they were doing.
The preposition "to" is unnecessary and grammatically incorrect after the verb "asked" when followed by an indirect object ("them"). The correct structure is "I asked them..."
Step 2: Identify the grammatically correct sentence.
Based on the analysis, option (1) is the only grammatically correct sentence.