how Postgres works internally
Bruce Momjian has an update of his old but useful presentation here:

http://momjian.us/main/writings/pgsql/internalpics.pdf

and there's Tom Lane's classic "tour of PostgreSQL internals" as mentioned by Neil, and the back-end flowchart that illustrates the high level overview of the execution path. 

Then take a look at the further resources here:


http://wiki.postgresql.org/wiki/Developer_and_Contributor_Resources 
http://www.postgresql.org/developer/


After reading the useful links from Neil and those mentioned above, your next port of call is:

https://github.com/postgres/postgres

i.e. the best reference is the source code. The PostgreSQL sources have some pretty detailed comments on data structures and functions, but you'll still land up wanting to attach a debugger and step through the code to see what it's doing sometimes. Use the tools provided, like the pprint function, to examine the state of the server.