You've been given plenty of good advice on how to handle missed deadlines: communicate delays early, stay aware of your progress, always be wary if unknown technologies are involved. Having that one covered, let me now tell you how to avoid them.
I've always been terrible when it came to setting deadlines for myself. Like most programmers, I often ignored "trivial" tasks and focussed on the interesting ones. But the trivial things are often the ones that make up the bulk of the work and also the ones that'll always come back to bite you in the ass at some point.
There's no way to easily account for these tasks. Instead, you should try to come up with an estimate for what you think you'll have to do and then double that estimate as a start (depending on how badly you miss your deadlines, you might even have to triple them). This will seem like plenty of time -- don't be mislead by your programmer machismo again, just stick to your estimate whenever somebody asks.
You'll most likely find that you end up with a deadline that leaves you plenty of room after you think you're already done. While it's great to finish projects way ahead of time, this is also likely to lead to false expectations. Instead, try to use some of that time for additional testing (you are testing your code, right?), documentation, refactoring and other polish. If you have time left after doing all that, feel free to hand in the project now. Few managers will complain if projects are completed too early.
Keep on doing this for the next couple of projects. Depending on how much time you have left after finishing the project (i.e. making it work), you might want to raise or lower your next estimates. As your perception of how much time you'll need is going to be highly subjective, nobody can really tell you how much you need to adjust that factor (I find that my factor usually lies between 50-100%, though it's shrinking now that my intuitive estimates get better). Just keep in mind that you'll always need to leave a small margin for unforeseeable problems along the road (e.g. delays, interruptions or personal reasons).
tl;dr: If your estimates don't work, double them and fine-tune them as you get a better understanding of how much time you need.