I'm afraid you just have to know which is correct for each word - there isn't a rule you can follow.
There is another pronounciation of 'th' - where it is pronounced as 't'. It occurs in some Proper Names, for example, the River Thames, or the names Thomas and Theresa.