It is advisable to take out a travel insurance policy no later than three days before the start of the trip in order to ensure that you can get compensated in case the trip is cancelled due to illness for example.
In other respects, the travel insurance is immediately valid even if you get it at the very last minute at the airport.