How do Google and Yahoo fetch content for pages that are behind a paywall or registration wall? I noticed that search results can show content that is not visible publicly without a paid or registered account, including in the page source and meta codes.
For Google to be able to fetch content from a paywalled site, that site's server software needs to be configured to serve up that content for free when it detects that the visitor is Googlebot.
In other words, when normal visitors visit the site, the server gives them the paywall, but when the Google crawler visits the site, the server gives it the full article.
How does the server know whether a visitor is Googlebot or not? The naive way is to check the visitor's user agent to see if the visitor identifies itself as "Googlebot" (or "Googlebot-News"), but then people can get through to your paywalled content by spoofing their identity (which is a trick that worked for surprisingly many years to bypass paywalls on a lot of sites).
As more users are using browser extensions to spoof their user agents to bypass paywalls, websites have started using a more reliable method to detect Googlebot - to check the visitor's IP address against Google's official list of IP addresses that Googlebot resides at. Checking this list is done over DNS by using a couple of DNS lookups. Google provides more details on this process in their article Verifying Googlebot and other Google crawlers.
If you want to make paywalled content accessible to Google, also be sure to use the proper structured data so that Google doesn't get it confused with cloaking, which is against Google guidelines. In practice, unless you're developing your own website, there will probably be a plugin for whatever CMS you're using that does all this for you.
Regarding Yahoo, the Yahoo search engine and crawler was discontinued several years ago. Nowadays Yahoo Search is just a frontend for Bing, and I can't find any resources on how Bing handles paywalled content.