Knowing and Hearing
When playing by ear there are 2 aspects. You play what you know and/or you play what you hear.
Let me explain.
This is very much like artists.
Artists go through many years studying anatomy to draw the human figure. They study the skeleton, the muscles, the tendons. Even though they know all this anatomy, when they do a work they will still often use a reference.
They draw what they know combined with what they see.
Likewise when I am improvising, composing, or transcribing, I play what I hear and what I know. When I am transcribing I can make good guesses based on what I know about theory.
What key is it in? What scales might they use? Does this sound like something I already know?
This is why folks with perfect pitch are not always the greatest musicians. They can hear great but they lack the knowledge. The two concepts are tightly related. Not quite the same. But not quite separable either. Much like the eastern duality ying yang symbol.
The light is inside the dark. The light also leads to the dark and the other way around. Strengthening you ears will lead to greater musical knowledge. Having more knowledge will also lead to better ears.