Stack Exchange uses a massive caching implementation to make all of this streamlined.
The hardware used is very impressive, expensive, and top of the line. This has always been a very large sticking point for Stack Exchange.
The core software approach used is to have a sliding time window which stores the composed SQL used to query the database. Re-using these queries, or execution plans, saves a large amount of time.
Often, when accessing a page, as a user you are not really asking for a large amount of data from the database. The largest amount of time is composing the query, paired with how long it takes for the data to make it over the wire.
There are some places where large data sets need to be accessed on the site, for example the review history, and those can take several seconds to complete because of the amount of data being examined.
In general, the content caching mechanisms in addition to the spectacular hardware are where the performance comes from.
As for all of the tools and gadgets used, there is a post which is maintained that details that on MSE Which tools and technologies are used to build the Stack Exchange Network? , and to see an analysis of how the caching works for views, I have a small writeup (contains some code) on MSE as well https://meta.stackexchange.com/a/224165/178816