I am asked, from time to time, to fill in with last-minute divrei torah; it happens on a weeknight between minchah and maariv, or at a minyan on Shabbos, or for a group of students in school. And I generally decline.
Certainly, I have delivered far too many last-minute divrei torah over the years to claim inexperience. But I believe that in general, the last-minute dvar torah is not a good idea.
The Shulchan Aruch (Orach Chaim 139:1) rules that one is not permitted to lein from the Torah unless he has reviewed the reading multiple times, out of respect for the Torah.
The mishnah teaches that Chasidim haRishonim [early pious people] waited for a time before davening shemoneh esreih, and we have psukei d'zimra for similar preparation, out of respect for Gd. How much more so when we are talking about teaching Torah!
Tefillah is about my personal relationship with HaShem, and perhaps the needs of those for whom I daven, and it requires preparation; giving a dvar torah is about the spiritual life and Torah knowledge of the entire tzibbur [community] - even if said tzibbur doesn't see it that way - how much more so is preparation required.
And it's not only about the stakes, it's also about the odds of success with a last-minute effort:
HaShem understands our heartfelt intent, other human beings tend to grasp only our words and expressions, if that.
HaShem is merciful, other people, often, are not.
HaShem wants our prayers; human beings are often subjected to divrei torah against their will and choice.
If I have a poor prayer, I blew an opportunity, but I have another chance later that day; if I give a poor dvar torah, they may never pay attention to me again.
Therefore, I want to spend time first thinking about what I will say, to whom I am speaking, how it will be received, where I should stand, whether the door should be open or closed, whether I should be formal or casual, and so on.
To me, and I know it's not everyone's view, I'd rather skip a dvar torah than deliver one without significant forethought.
That's my thinking, anyway. Make sense?