It's generally preferred to use the actual dialog audio from the take. It matches the movement of the lips, it sounds right for the environment, the actor is giving an authentic performance, etc. And, you don't have to pay the actor to come back for an extra day re-recording audio in a rented sound booth.
"Looping" in audio after a scene is shot is certainly not unheard of. If a line needs to be changed, or there was an error on set, or there was some unavoidable noise going on during filming. It'll also happen in very wide shots where it's hard to get a microphone close to an actor. If this happens, it's called ADR -- Additional Dialog Recording, or sometimes Automatic Dialog Replacement. Though as common as that latter definition of the acronym seems to be, the process really isn't particularly automated.
As for how to avoid needing it, you want to record clean sound from the start. This is why you'll have a boom operator pointing a boom mic on a pole as close to the actor as possible -- even at the risk of accidentally getting the microphone in the shot. If they kept the microphone far away, the actor would be quieter relative to all the other sounds going on in the room, so you'd have to crank up the gain and get more of those other sounds. You can also have the actor wear a hidden lavalier microphone. It's usually poking out near the collar close to their mouth, and then the wire runs inside the costume shirt so you can't see it. Occasionally if the character has a big hat or helmet, a lav mic can be hidden in there instead of by their neck.
While you are in the location, you also record some "room tone." Whatever the room sounds like when it's quiet -- because no place is actually 100% quiet. There's some wind, or the neighbor's air conditioner or the house creaks slightly. Whatever. Get a few minutes of that. And, if you have any crowd or whatever, you can get some of them talking or walking, or whatever it is that the other people are doing while the main characters are talking about the plot.
Then, when you get to editing the scene, you have a track of that "silent" room tone as a background noise. And you have you crowd noise. Maybe you use some actual recordings of the crowd from the set, maybe you just wind up using some stock sound, or do a separate audio recording session. If it's a cafe or something, you'll have some clinking cups and dishes and silverware, and some babble of people "talking" but you want to make sure that it's not any recognizable words that would catch your ear while you are supposed to be paying attention to what the main characters are saying. That can be just two or three tracks, or a whole bunch of tracks. If you are on a space ship, you may have a bunch of tracks of engine sounds and beeping sensors and whatnot. This bed of sound is constant through your scene, and is pretty much independent of the main character dialog that you get from the actual editing. (Unless the sensors need to beep extra loud when the aliens approach or whatever, that sort of thing obviously needs to be in sync with the video.)
With that quiet but consistent bed of audio, you can cut the scene and get the best takes, and use the dialog audio from the relevant takes without to much trouble. You don't notice any slight shifts in the audio from cut to cut because there is enough other audio that's consistent in that quiet background noise that it doesn't matter.