I think it simply looks up the number you are sending a message to. If it's a number/email address in Apple's iMessage database, then it uses iMessage servers to pass the message.
Other cool tricks to know about iMessage:
If you have an iPad, and you are using an email address with iMessage on the iPad, go to your iPhone and navigate to:
Settings -> Messages -> Receive At
Here you can add other "addresses" that your phone's iMessage will respond to. If you put your email address in here, then anytime someone sends an iMessage to your email address it will appear on both your iPad and your iPhone. If you have iMessage open on one of your devices and you are actively engaged in a conversation, the other device will stop ringing when a new message comes in. Don't you just love Apple's attention to detail?
In this same screen, there is an option called Caller ID. You can set what your device will use as a return address when you message someone. I went into this menu and put my email address in as my caller ID. As a result.. when someone replies to one of my message, it will always send to my email address instead of my phone number. This way I can read it on both my devices.