Most GA planes have a fuel shutoff valve which I don't quite understand the purpose of. Does it cut the fuel flow with the mixture. My theory is to prevent fuel flow at earlier stage for safety reasons?
It's both a maintenance and safety feature.
You need a way to cut off fuel flow to the engine compartment, either to work on the engine, or because of a fire at the engine, or because you are doing a forced landing and it helps reduce the risk of your entire fuel contents seeping onto your hot engine if you bend things a bit putting it down and a fuel line gets broken.
So you will always have a shutoff valve, and it's always somewhere upstream of the firewall.
Fuel shutoff valves are a hold over from the days of float style carburetors (and necessary on any float style carb system). With a float style carb when the fuel is on the bowl will fill up until the float floats up and shuts the flow off. Since gasoline becomes a vapor at ambient temperature the small pool in the carb will be constantly evaporating. As such the float will sink and refill the bowl. Given enough time this will drain the fuel tank. This vaporous fuel also fills the intake and can cause backfires or intake fires.
The mixture is adjusted post bowl and merely leans you to the point of cutout which may not truly be a full stoppage of fuel.
For fuel injected systems or systems that use some kind of blow through carb a shutoff valve is not strictly necessary for leak reasons but JohnK makes some good points on when its used for safety reasons. Some of these systems will also allow small paths for fuel to escape so a shutoff is often implemented to ensure total fuel stoppage.
The less technical, but accurate, answer is to meet the certification requirements of the FARs. Specifically, FAR 23.2430 is Airworthiness Standards for Fuel systems. It reads in part: (a)Each fuel system must-...(5) "Provide a means to safely remove or isolate the fuel stored in the system from the airplane"
Here are two examples of high wing fuel systems. The left is a from a 1973 Cessna 177, the right from a later model Cessna 172. You can see in the 177 the fuel cutoff is earlier in the system, before the fuel system is pressurized, while in the later plane it is after the fuel pumps. Either way, fuel is cutoff after the header tank to limit the amount of fuel available in case of an engine fire that could result from a crash.