Music is broken into major and minor keys, and from those keys other scales are formed. The key signatures dictate what notes can safely be played, meaning what notes will not clash with the rest of the music being played. Knowing your major and minor scales is the first step to effective improvisation. It's a good idea to keep a key chart handy to play regularly. You can purchase key charts in a music store or find them online for just about any instrument, free of charge (see Resources).
A highly effective improvisational technique is to limit yourself to playing arpeggios (the notes of a chord played separately). You should have a thorough understanding of how chords are built to do this. Try limiting yourself to using only the notes of whatever chord is being played, changing the notes you use as the chords change. Playing chord notes will ensure the notes you play go with the rest of the song, and by limiting yourself, you can force creative thinking.
Whatever instrument you play, one of the most effective improvisational exercise techniques is to play along with an instrumental track in various keys. You can buy instrumental tracks at a music store, or better yet, make your own by playing a repeated chord progression in various keys and recording them. Play the recordings back and improvise over them. Record the playback with you improvising over the chord progression and listen carefully to what you've played. You'll be surprised by how much of it sounds good. The more you do this, the better trained your ear will become.
There's no better way to practice improvising than to do it in a live setting. Try putting together a band and have regular jam sessions where the band plays a repetitive rhythmic groove and the members take turns improvising. Or seek out open jam sessions at bars with members who regularly play and allow others to join in.