If I contribute to a target fund that's targeting 2040 as a retirement date, but I don't end up retiring until 2043 what will happen after 2040? Will I still be able to contribute to the same plan after 2040? I don't know yet when I'll retire but probably sometime between 2040 and 2045. Would it be better to get the 2040 target fund or 2045 target fund?
Yes- you can still contribute to past date target funds. Really the only difference is how conservative the fund is. Come 2040, theoretically the past date fund would be slightly more conservative than its 5 year later counterpart, since the past date fund would try to minimize losses for those that are actively taking distributions. Of course this isn't universally true, but you can try looking at some specific days that had large volume changes to see how the various target dates were affected (and comparing them all the way down to 2020, 2015, etc.)
Also, you can change your fund allocation anytime, including well before 2040, or in 2039, or 2043, etc. It's not as if you're locked in to what you choose today. It's still your money after all.
Yes, you can still contribute, with the caveat that the fund company may eventually merge the target-date fund into a general "in-retirement" fund, in which case that is the closest thing you could still contribute to. You will likely want to reassess your investments anyway as you near retirement to make sure they will generate sufficient income, etc. But such mutual funds do not typically close to new investments.
The fund company fully expects that some people will continue contributing even to "in-retirement" funds due to working longer than nominally planned, or switching investments from a different fund family. Or if held in a taxable account, there could be many other reasons for purchases even after retirement such as having other sources of retirement income, or downsizing a home, or receiving an inheritance or other windfall.