I'll take a stab at this one-it is something I teach, although it's definitely not one of my strongest subjects(and it's the first thing right out of the gate in Gen Chem II, which I'll be teaching again in January...).
Basically metal atoms-any solid metal atoms-will arrange themselves into a a 3 dimensional regular structure that we usually call the lattice(effectively the crystalline structure). We can then examine a single cubit "unit" of the lattice, which we call a unit cell. There are several possible unit cell arrangements, and which one a metal will take AFAIK can't really be predicted but is based on atomic size and a few other factors.
A unit cell will have a certain size, and some unit cell arrangements contain more atoms per unit cell than others. Both of these will govern density, although of course the mass of individual atoms within the cell will play a big factor in that.
If you want a great overview of unit cells, this video helped me relearn it the first time I taught it, and I still re-watch it at least once a year. I'll add the caveat that it's assuming some chemistry knowledge and does get somewhat math-y, but is a great overview
What can get really interesting is when you start looking at alloys, and how adding in different metal atoms changes the unit cells. Steel is a prime example.
When you have pure iron, its unit cell is what is called body centered cubic(bcc).
What gets really interesting, though, is when you take pure iron and mix in carbon atoms, AKA making steel
Steel actually has several possible crystalline structures, most of which are temperature dependent. Some are bcc and others are face centered cubic(fcc) and even some more exotic shapes. What does happen, though, to give a hand-waving explanation, is that the tiny carbon atoms(relative to iron) can "fit" inside the lattice, which in turn disrupts its shape and makes steel less dense than iron. This is what gives it the relatively high strength/weight ratio we associate with steel.
Interestingly enough too, I mentioned the different crystalline structures of steel. I know of a few different types-the α form, which exists at RT and is bcc, austentitic, which is around 1000ºC and is fcc(there's a third bcc phase, δ, at even higher temperatures).
Of note, though, is that if steel is heated but not so hot as to leave the α phase and then quenched(rapidly cooled, such as by dunking a hot part in water), something that I'm guessing you probably know of in the real world
@billt460 especially as in making guns the steel often gets a LOT of heat treatment to give specific properties, you will get the Martensite structure, which has a complex structure that makes it quite hard and brittle. Basically heating causes the lattice to expand, allowing carbon to migrate into it, and then quenching "traps" the carbon where it wouldn't normally be located.
Further heating to more moderate temperatures allows some migration of carbon back out, which of course will reduce the hardness. How much it's reduced depends on just exactly how much and how it's heated. Depending on real world applications, you may want something to be extremely hard(when I have made watch parts, extreme hardness is often desireable especially on places like pivots, although too hard makes them more susceptible to breaking on impact). A gun barrel, on the other hand, you want hard enough that it is not appreciably worn by lead or copper passing through it at high speed but also not so hard that it will shatter as soon as its subjected to a shock like a lead or copper-clad lead projectile being shoved into it at high speed(backed by high pressure) that's just a little bit too large and is expected to conform to the shape of the barrel. You also want the other parts of the gun that are receiving the rest of the force to be hard enough to not deform but not so hard that they'll shatter. I'm guessing you've probably seen high speed videos of gun barrels with a bullet traveling down them, and the way I always describe it is that it looks like a snake eating a mouse since you will see the barrel "bulge" where the bullet is and then spring back to shape.
To go on a little bit of a tangent, too, especially since this is an interest of mine, as you probably know most pre-smokeless powder guns are considered unsafe to fire with smokeless powder. I know there are people who claim it's fine with light smokeless loads or whatever, but when I briefly owned a pre-smokeless Colt SAA years ago, you'd best believe the few times I fired it I had those 32-30 cases packed with Goex 3F under a soft swaged lead bullet-it was too valuable of a gun for me to chance it. As a little anecdote, that same day, I had my modern Uberti SAA in 45 Colt and had made up some black powder loads for it-I was at Knob Creek Range that day and at the time knew the regular, crusty old Marine RSO quite well. He trusted me to know what I was doing, and if me, or myself and my regular range buddy were the only ones out there, he'd basically leave us to our own device unless he was coming over to BS or occasionally do a bit of his own shooting. My day shooting black powder out of both of those guns was one of those days, and he sees the clouds of smoke coming from my usual spot down at the end of the range. He runs over and has some choice words/questions for why I'm even bothering, then basically says "You're the one who has to deal with the PITA of cleaning it when you go home, so have at it."
In any case, it's my understanding that in the early days, guns that didn't have deal with the pressure spikes of smokeless were often WAY too hard, and when fired with smokeless were susceptible to shattering.
It's also my understanding that in the teens, Smith and Wesson pioneered a lot of early metallurgical research into steel and documented a lot of nuances of different heating/cooling/quenching techniques that were previously unknown. They continued that on into the 50s and 60s. Back when I was active on the S&W forum, there was a guy who loved irking people by loading 38 Specials to "old" +P and beyond levels, and then shooting them out of 30s and older M&P revolvers. He also tried to convince me to load up something equivalent to the old 32-20 "rifle only" loads and insisted it would be perfectly fine to shoot out of my late production(early 1920s) 32-20 M&P, also insisting that modern factory 32-20 and recent loading manuals just assumed that essentially obsolete cartridges like that were now loaded with the assumption that they'd make it into an old BP framed SAA or the like. I never did-it may well have been fine but I liked the gun too much! Even though I have a system for marking my "hot" ammo-and hot meaning something like book max with Winchester 296 in 357 Mag or 44 Mag(I generally use Winchester primers since they are not nickel plated like the Federal and CCI I use on nearly everything else, or otherwise color the primer with a black sharpie and in all cases put an X over the case head...and generally write the exact load on the side of the case)-I pretty much only do that for situations where I know every gun I own in that caliber is perfectly safe with any book load I could throw at it, plus I really don't think it's possible to stuff enough 296 into any Magnum handgun case(maybe the 32 H&R not-really-Magnum excepted) to blow up a gun. 2400 probably wouldn't either It's been quite literally probably 5 years since I sat down at my loading press, and I really want to get it all out again and get back to it, but current space and more importantly time even to go to the range in the first place keeps has dulled my recollection a lot. I seem to recall around 24gr of 296 for 357 Magnum(or ~22gr of 2400) under a 157gr SWC and maybe 28gr or so for 44 Mag under a 240gr SWC. Any of those loads give such high case fill that I can't imagine going much higher. To my point on 32-20, though, if I really want something hot in 32 caliber, I have my much beloved but rarely shot Ruger Single 7 in 327 Magnum.
Sorry for the rambling. This post made me pull out textbooks and some other references while I was still working on my first cup of coffee. I'm going to be teaching a lot of this stuff soon(the metallurgy/unit cell stuff, not internal ballistics, although I wish I could the latter
and have given some impromptu lectures to interested single or small groups of students).