This is a question that even native speakers ask about:
http://detail.chiebukuro.yahoo.co.jp/qa ... 1013194790
The basic rule they give is that if こと is being used a common noun (普通名詞), use 事. If it's been used as a "dummy noun" (形式名詞), use こと. Having said that though, a lot of people these days will use こと even for common nouns.
So first, common nouns:
The word 事 has the meanings of 事件 (event) and 出来事 (happening) in it,so if you're using "koto" to describe a concrete matter, it can be written in kanji. One difference between this 事 and the こと described below is that this 事 can be the subject of a sentence (事が起こる).
Dummy nouns:
If you're using "koto" to describe not so much an event/matter but a more abstract "thing" (the こと in phrases such as そんなことはない、思ったこと、etc.), then koto should be in hiragana.
As you can see, it's a bit complicated (even native speakers are asking the same question..) but I hope that makes some sense