Question: When I do ping stackoverflow.com from command window in Windows, how does it get the ip address of stackoverflow. Basically how does the entire thing work ?

Answer: 
First it uses your local host file then it uses your primary dns server, if that fails it goes to the secondary dns server.
Take a look at the following...

<http://en.wikipedia.org/wiki/Routing_table>

<http://compnetworking.about.com/od/hardwarenetworkgear/f/routing_table.htm>

Also look up routing metric's

These are some fundemental networking