This problems plays on the theme of conservation of energy, a very universal concept that appears again and again in many facets of chemistry and physics. It can help to imagine these atoms are grape-sized magnets.
Here's the general premise when it comes to bonds in chemistry:
Breaking a bond requires energy and so the system undergoes an endothermic reaction (ΔH>0)
Creating a bond releases energy and so the system undergoes an exothermic reaction (ΔH<0)
Using the grape-sized magnets analogy: The system is the compound: in this example that would be the CO2. One magnet will be the C atom and the other will be the O2 molecule (O2 is more stable than a single charged O- atom and so is found more readily in nature). To break the bond that holds the two magnets together, you need to put energy into the system by pulling on the magnets with your arms. Once the bond is broken you experience a big relaxation as you no longer need to apply a force to separate the magnets; in that instance you have in effect neutralized the bond. The effort to break the bond is measured as the Bonding Dissociation Energy. These atoms that have absorbed their BDE will continue to move freely in space, their energy now manifested as Kinetic Energy aka motion. The energy of each free atom is higher now than when they were bonded together. This absorption of energy by the atoms is an Endothermic reaction.
Eventually, they run into another atom and reconnect to form a bond once more. These atoms have lower energy when bonded than when moving freely with KE. In order to conserve energy the atoms will jiggle in place (held by the bond) violently until they can dissipate that random energy. That nanojiggling of bonded atoms manifests as heat at larger scales. This process is an Exothermic reaction and involves the releasing of energy (heat)
In this example we have two halve step equations with measured values of ΔH, both of which are exothermic (ΔH<0) because they both produce a bond.
We can add these half-steps together to produce a larger reaction:
Diamond Eqn + Graphite Eqn = Diamond→Graphite
Note however, we will need to reverse one of the equations for the addition of these half-step equations to make sense.
Since we want Graphite as a final product, we will flip the Graphite equation around so that the C(graphite) is a Product rather than a Reactant:
CO2(g) → C(graphite) + O2(g) ΔH=+393.5
Note how ΔH is now positive as we now have an endothermic reaction involving the breaking of bonds
We are now ready to sum up these bonds (sum up the ΔHs):
C(diamond) + O2(g) → CO2(g) -395.4
+ CO2(g) → C(graphite) + O2(g) +393.5
Notice how the O2 and the CO2 will get cancelled in the final balanced rxn