I've often been asked questions about improvising; more specifically, my seemingly endless "idea box" that I keep pulling stuff out of to keep my solos interesting. Once I start to improvise, I can totally keep going with one different concept after another until someone either shoots me, or stops my bow in its tracks. I've had many set-up-my-phone-to-take-a-video-and-then-just-start-playing-whatever sessions; basically where I improvise for minutes on end without getting boring (usually 6 to 8 before I start being concerned about phone storage). Some of it comes down to just plain musical intuition about what the song needs at precisely that moment (and also technical capability to do that thing), but some of it actually is explainable. And since I have an opportunity to explain something and sound like I know what I'm talking about, I'll gladly take it.
(I do know what I'm talking about, don't worry.)
(Make sure you are fluent in not only the chords to the song you're improvising (or just chords in general if you're doing a solo freestyle thing), and switching between them smoothly.)
Before you are able to know what to do when, you have to have different things to do when. In other words, develop your "idea box". Here are some "idea box items" that I use (I have more, but it's too hard to remember them all at the same time) that will a) give you things to use, and b) get you started developing your own things.
Texture
Basically, it's just general variation in musical density. For instance, lots of notes played one after the other with no pauses (although I do recommend generally awesome phrasing), a very few long notes, short notes with a few beats in between, different rhythms than just straight eighths (syncopations recommended and loved unconditionally), and sometimes scalar runs all over the place (very dense).
Movement
There are different types of movement in improv. If one type of movement is favored over the others, the improvisation can become too "samey" too fast. Upward movement is exactly what it sounds like, a general noteward trend upward. Same for downward movement. Circular movement is basically a note "dance" throughout a particular range, and use of the same notes in different ways as the chord changes; this type of movement is usually in a range of about a 5th or a 6th and all the notes (diatonically or chromatically) within that.
Harmonic Intention
This is basically the type of notes you use over the chords. Options are traditional chord tones (1-3-5-7), extended harmonies (9-11-13 and all altered versions of these (probably only if included in the chord)), chromatic leading tones (into the root, 3rd, 5th, and 7th) and just plain chromaticism at its finest. Basically, this is choosing what kind of notes to play over a given chord and being able to play whichever type you want to in the moment; I recommend practicing all different types. They can be used at different times for variation, or mixed together for an entirely different effect.
Riffing
This is basically creating a melody (that's not related (usually) to the original melody), and repeating it and changing it when necessary to suit the rhythm and chord changes. Also try starting the riff on a different beat, or a part of a beat, each time. Riffs tend to be more groovy and rhythmic, than "notey" and technical.
Atonal Awesomeness
Sometimes during a dynamic drop of a song (or a drop I decide to create with one of my "idea bag items") I'll start improvising atonally. Just forget what chords you're improvising over, and focus on whole-tone scales, tri-tones, diminished arpeggios, different handframes, and a tonal center that even you can't find. It's a good idea to practice forgetting chords; it's very hard at first.
Melodic Variation
Exactly what it sounds like. Come back to the melody and do variations. The variations can be as far from, or as close to, the melody as you wish.
Patterns
Pick a pattern of notes (it can be scale, arpeggio, whatever) and repeat it. You can change the order, the timing, invert it, and start it on a different beat each repetition. Whatever you do, make sure you do it for a long enough time. It usually takes a bit for other people (bandmates, audience) to find the core of the pattern and groove with it, and THEN... CONTINUE doing the pattern because the amount of awesome is increased when the core is found. Stay on it longer than you think, but not too long.
That's most of the "idea bag items" I can think of at this time. As far as knowing what to do when, most of it comes down to actually having stuff to do when. Just remember that the solo should have contour and certainly should not be the same for any great length of time (there are exceptions based on musical context). Make sure to switch often (or switch often except for some times when you stay for longer than expected (it helps to keep it interesting) (the contrast between switching all the time and the occasional not switching is interesting because contrast is interesting)) and not let any one thing go too far. Remember to create dynamics within the song; use a less dense texture to make a quieter part (a drop in dynamics) and then ramp up the intensity with some atonal improv before doing a big crescendo using a gradual increase in note density. Or whatever. The point is, make it interesting.
In a nutshell: "idea bag growth" + switching between techniques at appropriate times + creative improv