"Playing songs by ear" is not a single skill - it's an effect of multiple different skills. They are, to a certain degree, interchangeable - you can rely more on ear, more on theory, more on just knowing the style of music etc,; you will not get a one-size-fits-all, but I can share my experience.
In the late 80s I was a pupil of a music school, playing classical piano. My dream was the same as yours - to be able to play "what I want" by ear. I got frustrated by the fact that my teachers seemingly couldn't do it and also - nothing in the curriculum seemed to be heading into that direction.
There were no books on the subject (I live in Poland) and no YouTube back then, so I just started to learn on my own.
At first I relied on rudimentary music theory and a small bag of tricks / procedure that I came up with myself (I will describe it in details below - who knows, it might still inspire someone today :) )
Then I got better at ear training and learned to rely more on that skill (at the beginning, I knew that when the song ends, I need to play the tonic - then I started to hear the required interval).
Then I started playing different styles of music, and suddenly theory and ear training were less important than knowing the vocabulary of that style. A simplified example: you can take the blues scale in C, practice a few finger positions - and learn to play really cool blues solos in a couple of days, without using neither classical ear training or any theory.
As I played more and more, I started to recognize whole building blocks - to the point where it was impossible for me to tell whether I play something because I "heard every note of it", or "know this building block by heart".
And so it goes. As time passes, a pianist gets to know more styles, more "building blocks". The music theory becomes something like grammar of a foreign language - you know it, but as you become fluent, you don't really need to actively think about it as you speak.
After more than three decades of "playing songs by ear" I am sure I will never be as good as I imagined I would be. It's a life-long journey and it's really impossible to plan it while making the first steps.
But, here's my the bag of tricks that helped me at the beginning:
- I would think of a melody (not sad),
- I would press "C" on my piano and sing the melody so that it ends with the sound of "C". This would usually ensure that only white keys would be necessary to play it (for sad melodies, replace "C" with "A" and be prepared to use #G); this is how scales work.
- Then I would find the melody by just trying all the white keys for each note.
- at the same time I would play triads with my left hand; there are just three "main" ones that should fit any "not sad" melody ending in C, and they are C, F and G major chords. With two exceptions the choice of the chord is simple - it's just enough to play the chord which contains the note played by the right hand (for sad melodies - use d minor, E major, e minor and a minor instead). "c" and "g" are exceptions - belonging to two chords at the same time. I would just try out both and decide which sounds better and always use C major at the end.
- not all melodies would work perfectly, but a lot would; all the little triumphs helped me get by.