ExplanationThe core idea behind the infinite procedural worlds in many popular games like Minecraft, Terraria, and countless others is something called "Noise". The term comes from signal processing. But in the context of procedural content generation (PCG), it is generally thought of as way to get random looking values that are deterministic. The fact that a noise function will look random, but reproduce the exact same value every time for a given input makes it perfect for PCG. Noise comes in a lot of different flavors. For PCG, you'll commonly see perlin or simplex noise. Red Blob Games created an eloquent explanation of how you can stack noise in different octaves to create realistic looking terrain. For my initial generator, I followed a very similar approach.
The trees and sand are generated with something a bit different. They get seeded using noise, but then any value below a certain threshold gets discarded. The remaining locations then get clumped together using cellular automata. The basic idea here is that every position looks at it's neighbors to determine if it should grow, die, or stay the same. The rules that dictate how a cell changes can vary wildly based on the desired outcome. You can even make an automata that resembles something alive. Here the trees are simply counting their neighbors and growing out if there are more nearby trees than the growth threshold. There is an excellent write up of how this process works on rougebasin.