In chemistry as in other sciences, I must begin by highlighting the fact that I claim no particular expertise. But I have seen a very good explanation of the answer to the exact question you have asked dlilpyro, and it seems to me that I am justified in giving this answer here because no-one else with greater expertise has yet provided it. If the explanation as I give it is imperfect, perhaps it will stimulate a more expert response. If it is wanted, I can provide the details of my source, but I can’t do it now because I am away from home at the moment
The first thing you should know dlilpyro, is that in point of fact, the answer to your question is not actually a chemical one, it is a physical one. It is to do with a phenomenon called ‘ground state’. Within any given circumstances, the electrons in an atom can occupy different energy levels, but the lowest energy level they can occupy in that circumstance and to which they will tend, is called the ground state. But electrons can find lower energy ground states in some circumstances than they can in other circumstances and where possible, they will tend to the circumstance that offers the lowest ground state. So, the reason that atoms of oxygen tend to pair into molecules of O2 is because the electrons in the oxygen atom find a lower ground state as part of an O2 molecule than they do as part of a lone oxygen atom. But that is not the lowest ground state that they can find. The lowest ground state they can find is as part of a molecule of carbon dioxide. That is why atoms of carbon and oxygen tend to form molecules of carbon dioxide when circumstances permit.