From where is archive.php getting its posts?

The main query is set up by WP class (curiously little known, since there is rarely reason to mess with it). It happens in between WP core finishing load and proceeding to load template, in wp-blog-header.php.

Note that some nuances of implementation:

  • WP class works on a global, set up earlier in wp-settings.php
  • the global it works on is $wp_the_query, which acts at original instance from which more commonly used $wp_query is spawned. This two-variable implementation is used for detection if the main query had been modified, among other things.