Essentially, if は is part of a word, it’s ha (pronounced with an H). If it’s behind a word, it is pronounced wa. There are two fundamental reasons why Japanese uses は despite it not being pronounced the way the hiragana acts normally. One, that’s how it was done, so it stays that way. Secondly, the “w” sound of は as a particle is not as strong as the “w” sound of わ. The “w” is enough sound to “articulate” the particle and differentiate it from the word before it, but not enough as to accent the particle.
The same occurs with を, which is “wo” when you see it in a word (not often), and “o” when used after a word (as a particle).