Generally speaking, you have to know which Kanji to use in which context. It is just like memorizing words, you have to learn the difference for each word with kanji variations.
At the same time, note the following:
In most cases (including the ones you listed), sentences will be understood if a wrong variation is used.
In some cases even native speakers ...