How does Android detect new mail, tweet, etc? I am curious about how Android is able to send notifications. I have installed apps such as Gmail, Google+, Whatsapp, Twitter, etc., how does Android know when there is new mail, whatsapp message, or Tweet? Is this something that is handled by Android or the application that is running?
What kind of things happen underneath?
It's a very clever system called &quot;Push Notifications&quot;.
How it works
An Android device will keep an active connection to Google's servers. This is a small connection, using very little amount of data and power. Notifications are sent from app developer servers, say Twitter, or Facebook, emails, etc. to the Google servers. The Android device will then see that notification and download it.
I just found a better explanation on Stackoverflow. The good explanation of how it works can be found here. (Here's a quote of the first answer, although answer two is easier to understand):

There is simply a TCP socket waiting in accept mode on a cloud Google server. The TCP connection had been initiated by the Goggle Play application. That's why Google Play must be installed on the device for making Google Cloud Messaging (GCM) (formerly Android Cloud to Device Messaging Service - C2DM) work.
When this TCP client socket receives some message, the message contains information such as the package name of the application it should be addressed to, and of course - the data itself. This data is parsed and packed into an intent that is broadcast and eventually received by the application.
The TCP socket stays open even when the device's radio state turns into &quot;idle&quot; mode. Applications don't have to be running to receive the intents.

Why using push notifications are useful
The effect of having a single open connection means two important things:
Firstly, your device uses less data/bandwidth connecting to all the third party servers.
Secondly, it uses less power, as applications don't have to be open in the background to check for notifications, as it's all handled by a single background application.