My primary instrument is trumpet, although I play guitar in p&w bands.
I was a jazz performance major & in simplistic terms, a good starting block would be to know the key & the notes in the scale. I find that the melodies that are used frequently stick largely to scale tones and do not employ many passing tones or non-scale tones. If you know the scale tones, you're in the ballpark.
Aside from that, in my experience, playing by ear on trumpet just comes from lots & lots of playing trumpet and lots & lots of familiarity.
Piano or guitar have easily observable scales and patterns, which can act as a guide to playing by ear. You hear a minor second interval, simply move your finger a minor second. You're right every time.
Conversely the trumpet has no readily observable patterns to the valve combinations. Hear a minor second and the next valve combination could be anything! When I play by ear, my fingers simply have been conditioned over time to instinctively know what valve combination to press to reproduce the sound that I hear in my head. No thought required.
So, start with scale tones. Practice playing by ear. It eventually begins to come around.