You can think of it like this: the weapons are containers, just like medkits and protective cases. To see what's inside these you need to hold them in your hand. This can be done by either having them in your hotbar and pressing the number it's on, or by dragging them from the inventory to the hand slot beneath your character's portrait. Once in your hand, you can remove and add things at will. You can right click ammunition to eject it, or if you use a weapon like the M4 with magazines you can simply remove that. I believe you can add and remove attachments from your weapon while it's on your shoulder, but I can't remember if you can eject ammo unless you're holding the gun.