Let's take some examples. On a few occasions I have had a musical idea and sat down and written it out in one session. This has happened with fairly short pieces and the flow of ideas is largely without much cogitation. On other occasions I have wrestled with a piece for days or weeks before finding a final realization. Let me cite a couple more examples of the intersection of improvisation and composition. The first is J. S. Bach. In May 1747 Bach paid a visit to his son, C. P. E. Bach who was court composer to Frederick the Great. Here is an article on that occasion. Frederick the Great had recently purchased six pianos and he gave Bach a chromatic theme on which Bach improvised fugues on each of the pianos. But two months later, after returning home, Bach sent to the king a fully-worked out set of pieces, known as The Musical Offering which included a six-part fugue on the theme. The implication is that while Bach was capable of improvising a fugue on a given theme--something few could do today--in order to achieve the finest musical results, he had to return to his studio to work out the details. Another example is from Beethoven. He was well-known as an improviser on piano, but at the same time he is also known for struggling for months and years to hammer out his compositions, sketching and re-sketching themes and harmonizations many times over.
I think that the conclusion must be that while some greatly gifted and knowledgeable musicians can improvise to an extent that is astonishing, the spontaneous nature of it is inherently limiting. In spontaneous creation one falls back on certain familiar patterns and techniques that would be avoided if you had the time to do so. Sometimes when improvising, one may come across by accident a novel idea which might later be worked out in a composition, but much of the time, improvisation probably results in fairly conventional patterns and structures. In jazz, for example, learning to improvise probably involves learning what sort of patterns fit within what harmonies.
I think the fundamental principle that separates composition from improvisation is what we might call a musical "Occam's Razor" --don't multiply entities needlessly. In improvisation, the tendency is to spin off phrases with abandon, which must often result in ideas that clash with one another. In a spontaneous context, no great matter. But in a composition, this is exactly what one avoids as it dilutes the focus of the piece.
These are obviously just my musings on the subject and I would love to hear contrary opinions from those who have more experience in the world of improvisation.
Here is the crown of the Musical Offering: a six voice fugue that Bach composed after his visit:
UPDATE: There are some interesting observations in the comments and one reader emailed to me the following: "I hope you (both) will pursue the question what characteristic (known retrospectively, of course) of a piece is especially recalcitrant to improvisation but more amenable to composition. Process theory, as far as I know, is also lacking in such an explanation (structural ontology)."
The phrase "structural ontology" itself makes me break out into a cold sweat because I almost understand what it might mean. But let me try and answer this because it is an interesting question. I would attack it by asking myself where the musical idea comes from? When you are composing, I have no idea. I have had ideas come to me at completely random times, though the shower seems to be a favorite place. Ideas just come. Then you have to sit down and work on them. That is a partly free-wheeling, partly thought-out process that is difficult to untangle. Now with improvisation, I think there is a lot of somatic involvement. Your fingers (in my case as a guitarist) just want to go to certain places. I suppose it is a kind of amalgam of somatic and auditory. In plain English, your fingers (or lips or whatever) have certain inclinations based on many hours of playing and this combines with what you are hearing either in what has just been played by you or others, or what you sense might be about to be played, and that comes together...
On reflection I see that I completely failed to answer the question which was, what characteristic(s) are recalcitrant to improvisation? I think the obvious one would be, as my anonymous commentor notes, "a complex structure that must fit at multiple scales requires composition: St Matthew's passion has structure at all time levels, from 3-second phrases to palindromic patterns at the "30-minute" level. " Yes, this kind of complexity really cannot be improvised. But why? I think the answer is that the only way to create a successful complex structure extended over a significant amount of time, is to have total control over every detail so that those details do not interfere with the unfolding of the structure but rather enable it. In other words, the design of a large piece of classical music depends on oppressing the improvisatory creativity of the musicians playing it. Heh! You don't want the flute taking a little solo during the rests in Beethoven's 5th Symphony.