Actually, It's amazingly simple.
1. You "listen" to the rhythm section, which is playing a series of "changes" which are, more or less, unique for each tune. This series of chord progressions is the harmonic structure upon which we will hang our "new" melody.
2. Begin by "singing" along with the changes. You may simply want to "ornament" the old melody, at first, until you become more comfortable with the process. Don't forget the various elements involved in creating a good melody: a concept of a beginning, (idea) an "expansion" toward a culmination point, and a feeling of having "evolved" (arrived) somewhere. Sequence, imitation at various intervals, and other developmental techniques should be utilized.
3. Later, as your compositional skills grow, you may be able to improvise several choruses without any reference to the "original" melody - sometimes called the "head".
4. One of the pitfalls, for the beginning improvisor, is to try to play everything one knows, within every chorus of every tune.
5. LESS IS MORE!
6. Equally sleep inducing, is the approach where one "lies in wait" for certain ISOLATED harmonic patterns - in order to play preconceived, or more likely, memorized "licks". (Often, what goes on "in between licks" is notably un-memorable).
The "other shoe" consists of developing your TRUMPET PLAYING skills to the point where you can instantly "realize" your "mental musical ideas" while playing.
If you are successful with PART ONE, PART TWO will no doubt follow. If not........!
Keep 'Em Flying!