Many people have a misconception about what "playing by ear" is. This is such a misconception, that it turns "playing by ear" into a magical, seemly impossible ability to accomplish as a musician. This is all just a music stereotype. It is unheard of for someone to pick up an instrument that they know nothing about and magically hear sounds from a song and are somehow able to play it back on the guitar. Anyone who says they play by ear, knows something about their instrument or at least music itself.
Playing by ear is simply a way of recognizing musical sounds and applying these sounds to the rules of music to learn to play songs much faster. Just as you are about to tell the different between the sound of a dog barking and a jackhammer, you can train your ear to tell the difference between chords such as majors and minors.