As you suggest, it seems unlikely that Aragorn and Legolas could compose such a poem on the spur of the moment. However, history - legend - tells us of courtly cultures where the ability to extemporize poetry was held in high regard, where song contests were held, and so forth. Contemporary jazz musicians can extemporize, though I suspect that some of this is composed in advance, then modified to fit the occasion.
Tolkiien presents a culture in which noblemen composed songs and poetry. Presumably Aragorn would have the skill. And funeral laments were part of the tradition of Middle-earth - cf. "Frodo's Lament for Gandalf," or much earlier, Turin's "Lament for Beleg."
However, it is quite possible that the "Lament for Boromir" was modled upon already-existing and well-known lament for a fallen warrior. It is much easier to extemporize in an existing form (making up verses for the Stanford Drinking Song, to cite a low example). If such were the case, Aragorn and Legolas, well-versed in the heroic tradition, might have been able to modify the stanzas on the spot to fit Boromir. (Remember too that it took some time to get the body to the boat - they could have been planning the funeral then.) This would explain Gimli's familiarity with the response to the East Wind, which he omitted. If such were the case, the funeral might well have taken place as described. And of course the scribe could always polish up the verses when he set down the accoutn some time later.