You can make a pleasing accompaniment to most melodies using three chords: I, IV, and V.
The reason this works is that between them, those chords have all of the notes in a standard scale.
For example, if the song is in the key of C major, then the I chord has the notes C, E, G, the IV chord has the notes F, A, C, and the V chord has the notes G, B, D. All seven distinct notes of the major scale are represented: C D E F G A B
So if the person singing or playing the melody plays a B, then you can play the V chord, and it will sound ok because B is in the chord. If they sing or play a G, you can choose between I and V because both chords contain the note G.
As you listen, you can get a feel for where the song is heading, and when the note G comes up, that prediction about where the song is going next helps you decide whether I or V seems like a better fit. For example, if it seems like the piece is nearing the end, you probably are going to encounter a V-I cadence or a IV-I cadence.
As you get more skilled, you can change it up a little bit, use different chords, make it more complex, but in a pinch, you can harmonize almost anything with I IV and V. There are exceptions, but for pop songs exceptions are rare.
Part of the neat thing about harmony is that there are many different ways to harmonize a melody that can be pleasing. So there are many possible ways to accompany a melody that will work. If there is more than one person accompanying the melody, they will have to coordinate a little bit. If they are in a band and have done this before, they will probably have a sense of which chord progressions the other band members like to use. It is possible for there to be mixups though, where some band members play one chord, and other band members play another. It is also possible to guess wrong about where a melody is heading and play a dissonant chord. However, dissonance is part of music, and if this happens, you can continue on to a more consonant chord with some filler notes and pretend you meant to do it all along.
So in summary, the accompanists probably don't know the chord progression for the whole song all at once. They probably can project a little bit into the future. They probably have a standard set of chords that they use in these situations, (For example, I, IV, and V, or another set of chords that covers all of the notes of the scale,) and they fit the chords to whatever notes the person sings or plays. The resulting harmony may not match the way that the person singing or playing the melody would usually harmonize the song, but it will sound pleasing nonetheless.