Having a good ear is going to help a ton when you’re playing with other musicians.
Really, it’s a part of listening.

Being able to read music is also crucial since then all you need is a sheet and you can play many songs of many different styles.
At a jam session, you could have a real book, but it’s likely you will be playing songs that you already know.
One simple exercise you can do is listening to recordings of melodies and simply playing what you hear. You might need to play back a small piece many times, but don’t worry your ability to do this will improve very quickly.
A step beyond that would be to play back improvised solos as you hear them.
