There is a common misconception on what "playing by ear" is. Many people think it is knowing what a melody is then hunting and pecking on your instrument for the correct notes. One wouldn't walk through a mine field like that so why would we attempt to perform like that? Because you probably know how to read and sound out words, I can say "cat" and you will be able to guess KAT or CAT or maybe even QHUAT. That is because you have mastered the alphabet and sounding out with your mind's ear.
True playing by ear is more cerebral than you think. It involves knowing all your scales, some degree of music theory and the ability to see the notes in your mind's eye when you hear them. Here is what I mean.
I can hear the tune ODE TO JOY in my head. Because I sing a lot but sing by visualizing sheet music (usually by the pool) in my head, I just know that the melody starts on the third. I can hear it start on the third. From there, as I hear each pitch, I just know what the numeric values are:
33455432 1123322 33455432 1123211
As I hum, I simply know those corresponding numbers because I only think in numbers. An added benefit to reading by numbers and not letters is that I can then transpose that into any key. Just start on the third of any scale and play those numbers. Poof, you're a genius. If I am improvising and I wish to quote this melody, I only have to aim for the third. There is no hunt and peck, there is no guessing, there is no "playing by ear." It is all brain conversion from inner ear.
The good news is that you can practice this anywhere without the need for a piano. Lying in bed, driving a car, lounging by the pool, hiking through the woods, listening to the homily . . . Also, whenever you hear music, don't just hum along, listen or ignore it, use it to practice. What is the starting pitch? Is it on the 1, 3, 5? Does the bridge change keys a fourth higher? What is the chord progression? You can hear all these things and translate them to numbers on the fly. WITH PRACTICE.
Some teachers teach the solfege method but I find numbers easier and if I am jamming with other musicians I can just hold up a finger. I can't hold up a SOL.
Try it with something simple such as MARY HAD A LITTLE LAMB. It starts on the third. Don't use just your ear, employ the brain. Don't guess, know. I'll wait. My brain tells me the pitches are 3212333 222 355 3212333322321. Did you get those too? Go test them on the piano in any key.
Now, someone is going to chime in saying "That is fine and dandy for melody but you can't do that with larger works, fugues or etudes." What they are really saying is THEY can't do it. Because I only read by numbers I can see all the numbers. If they read by letters, they see letters. I can't fly an airplane so therefore nobody can. All you need to do is know your scales and intervals and what each interval sounds like - away from the piano.
As an added bonus, you will find memorization to come rather quickly. Actually you are not "memorizing" notes. Your ear will hear and you'll just know or read in your mind. I don't have GOLDILOCKS AND THE THREE BEARS memorized but I can fake the story.
This will be offensive to many people but I consider letter readers to be musically illiterate. Like, they can speak but not read. My folk group musicians are all excellent readers. They can play anything I put before them. However, if I ask them to raise it a third, they can't. Why? They really don't know what they are doing. Matching dots to a key is so easy a parrot can do it. In the following video, I doubt the bird knows what a square, circle or isosceles trapezium is. This bird is not genius, it is just matching shapes like it was trained to do. BTW, this is how Beethoven could compose while deaf. His ear may have been deaf but his brain wasn't. Go ahead, "hum" something in your brain. What were the numbers?
Parrot matches shapes
A good way to practice is to go steal a hymnbook from your local Protestant church (God will forgive you) and away from the piano, number sing all four parts. Start with just one line at a time (SAT or B). Soon you will see all four parts by their numbers. Hymns are great to start with because they rarely go beyond and octave, the notes are repetitive, the form is often AABA and all the notes are chord tones. Study, study, study, study, study. Then sit at the piano. Oh, if you don't want to beg, borrow or steal, you can check out hymnary.org but taking a book with you means you can "practice" anywhere, anytime.