As dzero said upthread, every bullet type has its own story.
The general size and shape of the bullet is dictated somewhat by its use. Small rounds like the .22 are good for small game, but don’t pack enough of a wallop to reliably stop big game or a human. If you want something that hits harder, you’ve got a choice between bigger and slower or smaller and faster. Bigger slower rounds are subsonic, and don’t have the stability problems that can plague supersonic rounds, especially as they transition from supersonic to subsonic. The diameter is also only one part of the overall equation. The length and shape are also very important in the design.
There were some cases where the caliber was decided by the barrel. This usually meant that the round was being designed for an existing weapon though. For example, when they first made the Minie Ball (which is a conical bullet and not a ball at all) they simply took the Model 1840 and 1842 smooth bore muskets and rifled them. Since the existing muskets were .69 caliber (U.S. muskets were originally copies of French muskets, which were .69 caliber, and the standard stuck) this meant that the new Minie Balls had to be .69 caliber as well. In 1855 when they redesigned the muskets Another example is the .357 magnum, which was designed to be compatible with the existing .38 caliber round (the cartridge was .38, the bullet diameter was .357). The .357 and .38 super were designed to be just big enough to punch through the bullet proof vests that gangsters in the 1920s were wearing for protection. Guns that shot the new rounds had to be stronger and able to withstand greater pressures than existing .38 pistols, but the newer guns could fire both the new round and the old one, taking advantage of less expensive and readily available ammunition. The size for the .38 originally came from the metric size 9mm, which is a reasonably round number for a round roughly that size.
Generally speaking though, if you are designing everything from scratch, the barrel could be made in any diameter and did not generally dictate the size of the bullet.
The .22 was a very early cartridge, and was originally developed for indoor shooting galleries in the mid 1840s. The original cartridge was fairly low in power, and many variations, most with a bit more power, have been developed over the years. It became a very popular round and is still extremely popular because of its low cost. A lot of gun enthusiasts will do a lot of practicing with .22 rounds because they are dirt cheap, and the .22 is good for killing small game or removing pests from your farm and such. I don’t know exactly how they ended up on exactly .22 caliber, but it obviously ended up being a very good size.
.40 caliber is 10 mm, which is a nice round number, so it is easy to see how they ended up with that one.
The Colt .45 caliber was chosen because it was a replacement for earlier .50 caliber and they wanted to scale it back a little bit to make it more controllable in a pistol round. .45 is a reasonably round number that is smaller than .50 but not so much smaller that the round doesn’t have enough stopping power for what it was designed for.
That’s just a couple of examples, but you can see how every story is unique.