The OP seems to have a good background in syllable structure, but I'll give some background in that for those who might not.
This phenomenon is known as final obstruent devoicing. An obstruent is a consonant made by constricting airflow. In German, the relevant ones are (in IPA):
- stops (a.k.a. plosives): /b,p/ /d,t/ /g,k/
- fricatives: /v,f/ /z,s/ /ʒ,ʃ/
Each of the pairs above represent consonants that have the same place and manner of articulation; the consonants differ only in whether they are voiced or not. The voiced consonants are on the left and the voiceless counterparts are on the right.
When a voiced consonant becomes devoiced, it changes into its voiceless counterpart (e.g. /d/ -> [t]). Depending on the language, this can happen in a variety of different contexts. In German, the process is final obstruent devoicing because it occurs (always and only) in the syllable coda, i.e. the end of a syllable.
Since this process applies at the end of syllables, it becomes very important to be able to break down a German word into syllables; to do so, one must understand German phonotactics and be aware of morphological boundaries.
Phonotactics
At the beginnings and ends of words, the final consonants are always clustered together in a group. Therefore, you can always say that e.g. "d" at the end of a word is pronounced [t]. Word-internally, however, complex consonant clusters will make use of the surrounding vowels to break up the cluster (when possible, clusters must obey the sonority rules for that language) — it is basically taking the path of least resistance to articulating the word. For example:
- "Land" [lant] ([nt] cluster; one syllable, no choice)
- "Landes" [lan.dəs] (/d/ can move to following vowel)
- "sag" [zak] (one syllable, no choice; must be in coda)
- "sagen" [za.gən] (/g/ can move to following vowel, stay voiced)
- "sagten" [zak.tən] (/t/ in the way, so /g/ stays in coda and gets devoiced)
Word Boundaries
Oftentimes, a word is made up of two or more words; in these situations, the syllabification rules I stated in the last section do not apply.
Thus, compound words will not allow resyllabification across word boundaries:
- "Hand" [hant] (one syllable, no choice)
- "Hände" [hɛn.də] (/d/ moves to vowel [ə], stays voiced)
- "Handarbeit" [hant.ar.baɪt] (/d/ cannot cross word boundary, gets devoiced)
- "aber" [a.bɐ] (/b/ goes to following vowel)
- "ab" [ap] (/b/ must get devoiced to [p])
- "abändern" [ap.ɛn.dɐn] (/b/ still gets devoiced because of the boundary)
Special Case: Handlung
In your examples, you bring up a really great special case: Handlung. According to the rules laid out, the word is syllabified thusly:
/hand.lʊŋ/
It should therefore undergo devoicing:
[hant.lʊŋ]
But it doesn't. What's special about this word?
Note that there are other exceptions like this (examples from Zamma 1996):
- ebnen
- Gegner
- eignen
- neblig
- regnerisch
Notice what all of these words have in common? They are all words whose stems took suffixes that, in turn, caused a vowel to drop out:
- handeln + ung
- eben + en
- gegen + er
- eigen + en
- Nebel + ig
- Regen + er (+ isch)
Note that the vowel is crucial in syllabification and devoicing. Why does the vowel drop out? Because, if you imagine the intermediate word with the vowel intact, the first syllable gets main stress, the final syllable gets secondary stress, and the middle vowel, the "e", is just a tiny unstressed schwa sound that ends up disappearing altogether. At least, in writing it is deleted. But the voicing of the consonant in these words suggests that there is merely a nearly-gone schwa vowel that is very short, but still maintains the voicing contrast of these words.
Incidentally, reduction of unstressed schwa to nothing or nearly nothing occurs in many languages, including English: e.g. frighten/frightening. In English, the "e" vowel is simply not deleted in writing (at least not all the time).