This is an excellent and clear question. I have had this question myself many times.
Before living in Korea, I was in a 3-month intensive language training program for Korean. We were taught that the imperative form (which is the whole base of the question here) was 아/어/여 야 하다. However, when I went to Korea, I heard
아/어/여 야 되다 almost exclusively
I do not have any hard and fast grammar rule for you here, but in my experience in living in Korea for a few years and also obtaining a Korean degree in college, the verb endings
아/어/여 야 하다
아/어/여 야 되다
were used pretty much interchangeably. But they were not identical. Again, as I said, this is not a grammatical difference, but a difference in feeling or register.
아/어/여 야 하다 tends to be stronger and more imperative. It has a more emphatic feel.
아/어/여 야 되다 is weaker than the 하다 form. It is less forceful. I think that this is why it is more common; it comes across as less demanding.
I say this commonly, but a huge thing that hold up Korean learners is that they want a one to one rubric between Korean and English. They want to know exactly when to use each form. Black and White.
When to use each of these forms is more a matter of feel and less a matter of right and wrong. They are grammatically equivalent; however they show a different level of imperative necessity.
Take the example sentence as follows:
We need to go.
How do we translate this into Korean? Well, how imperative is it? Written English is somewhat deficient in this regard. The only way to know the true emphasis intended is to hear the sentence spoken.
But now look at these two sentences:
"We need to go now." versus "We need to go. Now."
The written emphasis is slightly different. The second sentence shows more imperative necessity than the first.
In my experience, the two sentences above show a good example of how to use 아/어/여 야 하다
아/어/여 야 되다.
Translated, the two English sentences could be written
"가야 돼요" versus "가야 해요"
The second one has a feeling of emphasis and higher necessity.
(I will leave the discussion of which 체 [register or politeness level] to be using here for another day. There are cases for both sides).