Learn how to "jam"
to jam: to play improvised or unrehearsed music with others
Intro & the basics 
This post is most applicable for guitar, bass, and piano. This also applies to the music of western countries which has a total of 12 unique notes (12-TET). To be differentiated from e.g.: Arabian music (24-TET), or others (19-TET, 31-TET).
Assuming that you're a "beginner", start by researching the proper technique to handle your instrument (e.g.: guitar + proper technique) before continuing. You want to play efficiently, not sloppy. Try to understand the logical reasons behind the technique and don't practice without it (it will eventually become natural to you after months of practice).
Technical terms & study 
Research the following terms and information. Also take notes of any synonyms you may encounter.
- half-step —— the distance(interval) from one note to the next possible note higher or lower in pitch
- whole step —— two half-steps
- sharp ( # ) —— when you see this next to a note it means you play that note a semitone (half-step) higher from where it's usually played
- flat ( ♭ ) —— the opposite of #; a semitone lower
- scale —— a series of notes used together to produce a specific style of music. The use of scales easily allows you to create melodies, chords, and solos since it is a fabricated model of all the notes that work together. Almost every song uses a scale (mainly the major scale and the minor scale).
- chromatic scale —— Is all the types of notes in our music C C# D D# E F F# G G# A A# B C (keep in mind that C# is the same as D♭, and G# is the same as A♭, etc...)
- interval —— the difference in semitones between two notes
- scale pattern —— a pattern of intervals used to remember scales and their type (e.g.: 2,2,1,2,2,2,1 major scale pattern)
- scale patterns:
- 2,2,1,2,2,2,1: (Natural) major scale (frequently used as a reference for chords)
- 2,1,2,2,1,2,2: (Natural) minor scale You may proceed to the next section. Explore these other scales later.
- 2,1,2,2,1,3,1: Harmonic minor scale
- 2,1,2,2,2,2,1 ascending and (natural) minor scale descending: Melodic minor scale
- 2,1,2,2,2,1,2: Dorian scale
- 1,2,2,2,1,2,2: Phrygian scale
- 2,2,2,1,2,2,1: Lydian scale
- 2,2,1,2,2,1,2: Mixolydian scale
- 2,1,2,2,1,2,2: Aeolian scale (same as (natural minor scale)
- 1,2,2,1,2,2,2: Locrian scale
- 3,2,1,1,3,2: Minor blues scale
- 3,1,3,1,3,1: Augmented scale
- 2,2,2,2,2,2: Whole tone scale
- 2,2,3,2,3: Major pentatonic scale
- 2,3,2,2,3: Yo scale
- 3,2,2,3,2: Minyō scale
- 1,4,2,1,4: In scale
- 2,1,2,1,2,1,2,1: (Whole-half) diminished scale
- 1,2,1,2,1,2,1,2: Half-whole diminished scale
- 2,1,2,1,2,2,2: Half-diminished scale
- 1,3,1,2,1,3,1: Arabic scale
- 1,3,1,2,1,2,2: Phrygian dominant/Spanish Gypsy/Jewish scale
- 2,1,3,1,1,3,1: Hungarian gypsy scale
- 2,2,1,2,2,1,1,1: Bebop dominant scale
- 2,1,1,1,2,2,1,2: Bebop dorian scale
- 2,2,1,2,1,1,2,1: Bebop major scale
- 2,1,2,2,1,1,2,1: Bebop melodic minor scale
Finding the notes on your instrument 
Before you continue, do a research online or anywhere to find the name of all or at least one single note on your instrument (e.g.: C).
Let's look at all the types of notes again: C C# D D# E F F# G G# A A# B C. Notice there isn't a note between B and C, and, E and F. You can use that fact in order to unveil all the notes of your instrument. Let's say you start from a familiar note (e.g.: C ). Then all you need to do is play the next possible higher pitch (one semitone higher) to find C# and so on until you have your entire instrument mapped out.
Keep in mind there's more than just 1 of the same kind of note ; look (i.e.: C, C#, D, D#, E, F, F#, G, G#, A, A#, B, C, C#, D, D#, E, F, F#, G, G#, A, A#, B, C, C#, D, etc...) once you reach the end (G#), it loops back around to A. In this example there are 3 C notes yet they are all at different pitches.
Using this knowledge 
Jamming is all about experimentation, although it helps to have a basic understanding of musical concepts. You'll need to practice your hearing in identifying different types of scales. Just familiarize yourself with the minor and major scales to begin with. Basically the minor scale will sound more dramatic than the "happy" major scale.
In order to jam with someone, you need to identify what type of scale (minor, major, etc...) the song is in. You do this by trial and error while keeping in mind the patterns for those scales; 2,2,1,2,2,2,1 (major), and 2,1,2,2,1,2,2 (minor). So start on any note, sound it out, and determine whether it sounds right or not. Once you have one note, keep guessing from thereon by trying to move up a half-step or a whole step, it's one or the other. Say it was a half-step '1', then you know it continues either as '2,2' or '2,2,2' if we look at the patterns. You'll be able to identify the scale once you determine that it's either 2,2,1 or 2,2,2. Memorize the notes you found and play them in a random order that suits you, that's called "soloing" or "creating a melody". If you make a mistake you can easily quickly correct that mistake by moving up or down a half-step or by ascending or descending chromatically from that point.
So there you have it. Once you know scales and how to identify the scale of a song, you have the ability to "solo", to "jam" with it by playing random notes (whatever sounds good to you) of that scale at once (harmonically/chords), or one after the other (melodically/melody). You improve your jamming skills by improving your ability to guess the distance (interval(s)) of the next note or series of notes as how you sing it in your mind. Practice yourself to imagine the next note or melody that would sound good and guessing where it is or how far it is from your current note. Once you're satisfied with your guessing skills with melody, move on to chords.
See also 
- Blues basics (for guitar)