Consider the BMW G20 3-series petrol cars. Essentially all trims except the M340i have twin-turbo 2-litre inline-4 B48 engines. The engine output is tuned by the engine control unit (ECU, which is a bit of software running on an embedded computer) depending on the trim: 318i gets 115 kW, 320i is 125 kW, 330i is 190 kW. There are occasional hardware differences—typically the intake manifolds, radiators, and maybe belts, but the engine block is exactly the same across these trims—they're all B48. This is public knowledge; there is a detailed table on Wikipedia[1].
Engine 'tuning' is exactly tweaking numbers in the ECU. A stage 1 tune could involve mapping a 318i to a 320i or better without even popping the bonnet; just a laptop plugged into the OBD2 port above the driver's foot rest.
Naturally, as mentioned by others, some of these tunes are less than legal, because the price, road tax, and other legalese surrounding motor vehicles are determined by the performance and fuel economy of the vehicle from the factory. A car is so computerised that nowadays there are plug-in 'chip tuners' that remap the ECU signals on-the-fly and just-in-time, and can be removed before inspections. It's not dissimilar to overclocking computer chips.
This applies across all car makes, not just BMW and not even just European ones.
[1]: https://en.wikipedia.org/wiki/BMW_3_Series_(G20)#Petrol_engi...
The idea that those engines don't differ is an urban myth that exists at least since the mid 1990s and the first TDIs.
Of course they differ in more than one way and using the higher configuration can easily destroy them.
Similarly, the car I have as a track toy, a 1986 Porsche 944 Turbo, was famously limited by Porsche to avoid competing with their flagship 911 Turbo. Tuners figured this out and came up with all sorts of modifications, from mild to wild, to extract more performance.
Back in the 2000s I didn't have a problem with insurance. Probably not the same today.
No, it is typically for emissions and fuel economy purposes. Engines are pretty consistent in specs when they come off the line.