This happens all the time, because the melody notes are often chord tones.
On piano, if you are playing the melody (no singer, for example), we often want to play the chords underneath the melody. That helps us hear the melody, because it's the highest part.
This is where inversions come in. If the melody has a B, then we want to choose a voicing of G which has B as the top note. Beginning students often just play chords in their root position, which resulted in a disjointed sound. If you choose your inversions to fit the melody, you'll get a much smoother sound.
If you're not playing the melody (because someone else is), you have a little more flexibility. In fact, you may want to avoid the melody notes, so that you don't make the sound too muddy. With experience, you'll figure out the appropriate style of accompaniment for each song you play.
So, for now, try and choose an inversion where the melody note is the top note of the chord. As you get more experience, branch out experiment.