As a woman who is inching towards 40-year old, I think I am finally qualify to write an essay on how not to freak out when you turn 30/40 (freaking out when you turn 30. Hah!) A few things to keep in mind:
1) Don't try to compare yourself with someone in their 20's. They are in their 20's, you are not. Keep in mind that everybody will eventually turn 40, barring untimely deaths, which nobody counts on.
2) Don't focus on "Oh my God, I am turning 40," instead, focus on your accomplishments so far. Unfortunately, not everyone can be rich and famous -- that would just cause ridiculous inflation -- hopefully you will be able to find some reasons why the first half of your life was not entirely pointless.
3) Don't tell yourself what you can't do because of your age. Be realistic with limitations, but for the most part, not a whole lot are entirely out of reach. I find a lot of inspiration in the 50+ individuals who go onto Wipeout. It's 40, not the end of the world.
4) Be young in spirit, keep your mind active, keep your body active, pay attention to a healthy life style, and most importantly, keep your sense of humor -- being old doesn't mean you also have to be boring. :)