Hi,
If I can hum the tune then the chances are that I will be able to play it, If not, it is trial and error to pick the correct intervals between notes and listen to hear if it sounds right. I have enough experience to have a good idea of what chords to choose to fit the melody.
The first thing to do is learn the diatonic chords (Using notes in the scale) for the key you wish to play in.
For instance, the scale of G major, using triads only would be G, A min, B min, C,D, Emin, F# dim and back to G
These are the most likely chords but there are often notes and chords outwith the scale as well.
If I want to learn a tune from scratch
1, I play the tune on You Tube and find out the key.
2, Listen and pick out the bass,
3, Fit the chords to the bass whilst humming the tune
4, Pick out the melody and play it
5, Put it all together.
I hope this helps.