Routers build the Routing Table with the help of Routing Protocols like RIP, OSPF, EIGRP, BGP, etc. You can refer to the Internetwork Technology Handbook for detailed explanation.
http://www.cisco.com/en/US/tech/tk1330/tsd_technology_support_technical_reference_book09186a00807594e5.html
Refer the chapters:
Routing Basics
Enhanced Interior Gateway Routing Protocol (EIGRP)
Interior Gateway Routing Protocol (IGRP)
Open Shortest Path First (OSPF)
Routing Information Protocol (RIP)
Border Gateway Protocol (BGP)
Bridges and Switches form the MAC Address table using a procedure called backward learning. The procedure is explained along with a good demo at
How LAN Switches Work
http://www.cisco.com/en/US/tech/tk389/tk390/technologies_tech_note09186a00800a7af3.shtml