"Tous les mains" is indeed invalid as a unit (since "mains" is feminine), but it is nonetheless a valid sequence of words in this sentence because it is not being used as a unit. "Tous" is an pronoun, not an adjective here. It is not connected to the following noun phrase; rather, it refers back to the subject "ils" (with which it agrees).
This may become more apparent if you try to translate the text to English: "They raised all their hands to heaven" doesn't make as much sense as "They all raised their hands to heaven.../All of them raised their hands to heaven..."
Note that the pronoun "tous" is pronounced /tus/, with the consonant sound /s/ at the end, unlike the adjective "tous" which is /tu/ before consonants and /tu.z/ before vowels.
Here is a thread on the Wordreference forums that you may find helpful: FR: tout / toute / tous / toutes