Facebook and Gmail constantly poll the server with Ajax Requests in certain intervals. If the server doesn't respond in a certain time, they will give the error message. Simple as that.
Easy example:
window.setInterval(function() {
try {
myFavoriteAjaxLib.poll('/server/heartbeat/time/1234567890');
} catch(e) {
alert('Something went wrong');
}
}, 5000);
Of course, you will have something more sophisticated in the real world. You will see the heartbeat thing quite often though. SO uses one too. Type the following into your browser's address bar:
javascript:for(i in heartbeat) alert(i +':'+ heartbeat[i]);
or inspect the DOM with Firebug.