Why do some chemical reactions release energy, while other chemical reactions absorb energy?
Question is so general that the only answer general enough is "because that's the way it is".
On the most general level - it depends on the difference between bond energies in products and reactants. Sum all bond energies and you will see whether you can gain energy or you have to put energy in.
It has been a while since I had chemistry, but I believe all chemical reactions can be either exothermic or endothermic depending on which direction it is run (ie, they are reversible). For example, this common reaction:
H2O + E <=> H2 + O2
Notice the symbol in the middle, which has arrows pointing in both directions, indictating the reaction can be run in either direction.
The premise of the question would therefore seem to be incorrect...
Right On! Hydrogen burns in an oxygen atmosphere and releases heat + water.
In the upper atmosphere, cosmic rays and solar irradiation ionize water and produce hydrogen and oxygen.
In photosynthesis, part of the process of making c6H12O6 from CO2 and H2O is the dissociation of water by light photons.
As with all physical processes, chemical reactions tend to prefer moving toward a lower state of energy. Just as a ball rolls down a hill because rolling downward lowers its gravitational potential energy and releases that energy as kinetic energy, many chemical reactions occur because the products have lower chemical potential energy than the reactants. The excess chemical potential energy is released as heat.
Therefore, it would seem counterintuitive for some reactions to absorb heat. In these cases, the products have higher potential energy than the reactants and would seem to correspond to a case where you see a ball spontaneously rolling up a hill. However, this discussion of chemical potential energy (formally known as enthalpy) omits another important driving force for chemical reactions: entropy.
Entropy is a quantitative measure of the amount of disorder in a system, and all systems would prefer to increase their entropy (i.e. increase their disorder). Many of the endothermic reactions that we observe (i.e. the reactions that absorb heat) are driven not by the desire to minimize chemical potential energy, but by the desire to increase entropy.
A good example here is the melting of ice. In order to melt ice, you need to put in heat energy in order to break the intermolecular bonds that hold water molecules in their crystal lattice. Thus, the melting of ice is an endothermic process. However, if you put an ice cube on the table in your house, you should observe that it melts spontaneously. Here, entropy is at work. The crystalline lattice of the ice cube represents a highly ordered structure, whereas the loose jumble of molecules in a liquid represents a state with considerable higher entropy. Thus, the melting of ice increases entropy and this increase in entropy is enough to offset the increase in chemical potential energy.
It is also important to note that this competition between minimizing enthalpy (chemical potential energy) and maximizing entropy is dependent on the amount of thermal energy available in the system (i.e. the temperature of the system). For example, if you lower the temperature below 0oC, liquid water will start to form ice. Therefore, at these lower temperatures, the release of heat due to freezing outweighs the loss of disorder.
Separate names with a comma.