Here's a good explanation in Swedish:
Many write "dom" as most of us pronounce it. But at least in the formal written language "dom" is a colloquial form which is not really accepted. Instead, the written forms "de" and "dem" are used. In formal texts; articles, argumentative texts and investigative texts "de / dem" should be used.
So when should you write "de" and when should you write "dem"? The grammatical explanation is that "de" is the subject form and "dem" is the object form. So if "dom" is a subject in the sentence, you should write "de" instead, but if "dom" is an object you should instead write "dem."
Then how do you know if a word is the subject or object? Simply put, the subject is the one who does something, while the object is that which is exposed to the subject's action.
I hope this helps a little :)