The quick answer is - any time! A doula who is available around your due date will be happy to work with you no matter when you hire her. I've been hired as early as 10 weeks and as late as 37 weeks.
Ideally, you would start researching and contacting doulas as early in your pregnancy as possible. This allows you time to look at websites, make phone calls, and set up in-person interviews with doulas that you think might be a good fit. I encourage everyone who interviews me to talk to several doulas so they can find the person that they "click" with! Every doula has a different personality, and some doulas have additional training or certification. Find the one that works best for you!
The earlier you hire a doula, the more time you have to get to know one another, and a resource to rely on throughout your pregnancy for questions and resources. I talk a lot about birth options with my clients that they were not aware of before we chatted! Also, a doula who knows her area well can tell you the best birth locations/care providers, put you in contact with childbirth educators, etc.
A doula may be unavailable if she has already taken a client or two around your same due date, or if she has personal plans. Holidays are tricky times to schedule; if you are due around Christmas/New Years, you should definitely start looking for a doula early!
What if you are not early in your pregnancy and you just heard about doulas OR just decided that you do want to hire one?
You may have to call or e-mail around to find a doula who is available during your due date, but it is not impossible to find one. Even a doula who is unavailable may be able to direct you to a few names of other doulas who are available.
Ideally, you want to have time to meet with your doula once or twice before your birth window (approximately 38 weeks - 42 weeks) so that you and your partner can get to know the doula and discuss your birth preferences. A doula will schedule these prenatal meetings at times that are convenient for you! You can spread them out or have them close together.
There is no reason why hiring a doula late in pregnancy is a bad idea. You still have all the benefits that a doula provides - emotional, informational, and physical support for your and your partner before, during, and after labor!