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.