If i remember correctly, you use onyomi when the kanji is used next to another kanji and kunyomi when next to kana only
As a rule of thumb, that’s generally true, but there are exceptions. For example, 神々 is a kun-kun compound, and there are mixed-reading compounds like 重箱 and 湯桶. Furthermore, some compounds like 市場 have dual readings, one on’yomi and the other kun’yomi. So the only sure method is to learn each word.