For example, on bofors anti aircraft guns and some japanese ww2 machine guns I've seen, the barrel widens out slightly at the muzzle. Whar's the purpose of it and why don't all gun barrels have it?
Those flared muzzle devices are called flash hiders. Flash hiders do what the name implies - they mitigate the muzzle flash visible from the sides and rear, and they tend to appear in a number of circumstances.
The whole reason a flash hider is needed in the first place is because of the impact the flash has on the shooter - a bright flash can disrupt the shooter's vision, and in low-light conditions it can seriously impact their night vision. Unmitigated flash is a particular concern in shorter barrels, as the powder has less time to burn and there can be a significant amount of combustion happening after the powder has left the barrel.
In the case of the Bofors guns and other antiaircraft guns, the purpose is pretty straightforward - these guns have high rates of fire and the continuous muzzle flash can make it difficult for the gunner to track his target unless it's mitigated in some way. On smaller guns, there's the Lee Enfield No.5 Mk.1 "Jungle Carbine," which has a flash hider because the significantly shorter barrel of the carbine leads to a significant flash compared to the standard short rifles.
As for why it's not a feature on all guns, there's a number of reasons. The simplest is cost - muzzle devices require extra work and expense, and, as with many niche features, it's generally not included unless deemed necessary. In recent years, flash hiders have also evolved into more compact designs. Many military service rifles incorporate flash suppressors - including the M-16 and its variants - that are distinctly different from this conical style. The "birdcage" style on the M-16 has a few benefits over the conical style of the Jungle Carbine - it's smaller and lighter, and it makes bayonet design simpler (loop over the barrel is smaller). In addition, it's designed to be able to fire NATO 22mm rifle grenades, although these are rarely used in practice.
The other reason is that there's often a tradeoff for something else. Muzzle brakes are another common muzzle device, though they're often difficult to distinguish from a modern flash hider. Muzzle brakes divert some gases off the axis of the barrel to mitigate recoil. This comes at the cost of increased noise and pressure to those around the shooter, but it can provide significant mitigation of recoil.
Examples of muzzle brakes can be seen on most artillery pieces. The distinctive arrowhead-shaped muzzle device on the Barret M82 sniper rifle is a more extreme example of a muzzle brake on a man-portable rifle, while more subtle designs include the slant-muzzle brake on the AKM.