Great question. I'd like to give you the long and the short answer.
The short answer is that you purchase the title insurance at the closing. It's one of the many charges paid on the HUD-1 Settlement Statement.
The long answer is that you should shop for your title insurer at the same time you shop for a mortgage or select a property. Price of services and quality of examination and service vary.
Once you have selected your title insurer, control the order. Tell your real estate agent and mortgage lender who you wish to use. Place the order then work with your title insurer through the title commitment review and closing. They will issue the policy after your documents are recorded.