where is the code that executes the template hierarchy?

wp-includes/template-loader.php is the file which is the template hierarchy. However, by the time that file executes, all the template decisions are already made since all that file does is check for boolean flags (defined in wp-includes/query.php function init_query_flags).

So all the heavy-lifting and interesting code happens before that at wp-settings.php line 225:

$wp_the_query = new WP_Query();

and it is definitely inside of the WP_Query class (starting with the constructor in wp-includes/query.php line 2998) where all the interesting logic happens.