I'm foreign and this causes me a lot of trouble. Sometimes the 'd' is pronounced normally [d], for example - done, but sometimes it's pronounced as [dʒ], for example - dream. How should I pronounce words like these? Is there a general rule to this?
As Peter Shor said in a comment, words with the tautosyllabic consonant clusters /dr/ and /tr/ (almost always spelled <dr> and <tr> respectively) can be pronounced in a way that sounds like [dʒr] and [tʃr] in a number of English dialects. The exact details of the pronunciation, whether it also applies when the "d" and "r" were originally in separate syllables (e.g. "bedroom"), and other stuff like that vary—see What is the IPA for "trade"? for more discussion.
There are also some other contexts where the letter D corresponds to sounds other than a simple [d]. D may be pronounced [dʒ] when it comes before <u>, as in education or for some (mainly British) speakers dune, and in a few words where it comes before <i>, as in soldier. This is the result of "coalescence" of a historical [dj] sequence. In American English, words like dune are usually pronounced with /du/ because the sequence [dj] was simplified to [d] at the start of a stressed syllable. This is called "yod-dropping": other prior questions that ask about this phenomenon are Is the pronunciation difference between “BrE deuce” vs “AmE deuce” systematic? and Why the does 'tu' get pronounced 'tyu' in British English?