I guess this is a very general question of what makes certain elements
combine the way they do to form compounds.
At the fundamental level, I think, a good answer to this question would be the principle of minimum potential energy, which basically states that a system has a tendency to be in a configuration that minimizes its total potential energy. (Please, note the word tendency!)
For the case of molecules it means that if forming a molecule out of atoms lowers the potential energy comparing to the situation when these atoms are infinitely far apart of each other, than there is a natural tendency for this molecule to be formed.
But there is a one important detail that complicates matters: given some set of atoms, there is no guarantee that there exist only one possible arrangement of them which minimizes the potential energy. In fact, the situation is usually the opposite: there are many different minima in the potential energy corresponding to different possible arrangements of the same set atoms. Often we can clearly identify one global minimum (the lowest one) and call all other minima local minima.
Any local minima in potential energy (directly on indirectly, i.e. through other local minima) is connected with the global minimum in a sense that there exist some way for a system to transit from a local local minima to the global one. But on it is way to the global minimum from a local one potential energy first rises and only then falls. It falls to the lower value, but first the system need to overcome a barrier. Thus, we say that a structure that corresponds to a global minimum is stable, while structures that correspond to local minima are metastable in a sense that a system (a molecule) actually may reside in these local minima for quite a long time if the barriers are relatively high. Relatively to what? Basically, to the amount of kinetic energy a molecule posses so that it can be converted to the potential energy to overcome the barrier.
A good analogy may be drawn with a ball resting in a hollow on a slope (image below courtesy of Wikipedia)

If the ball being in a hollow (1) does not have enough kinetic energy to be converted into potential with the aim to overcame the barrier (2) and move on to the lower energy minimum (3) it will stay at (1) for ages. But if the ball has enough kinetic energy it will roll to the lower level. Here (3) might be just another local minimum or the global minimum, it does not matter. If (3) is not the global minimum then this process is just one of the steps on a way to the global minimum.
Now, back to molecules, think about (1) as of 6 molecules of formaldehyde being far apart of each other and about (3) as of 1 molecule of fructose. Both these different combinations of atoms correspond to minima in potential energy, and here I assumed that fructose is lower in potential energy. Note that I could not guarantee that, as well as, I could not guarantee that of of these minima is the global minimum for a system composed of 6 atoms of $\ce{C}$, 12 atoms of $\ce{H}$, and 6 atoms of $\ce{O}$. Apriori we do not know what form is lower in potential energy, so we have to do some calculations, say. But here let me just assume that $\ce{C6H12O6}$ is lower in potential energy than $\ce{6 CH2O}$.
Now recall that (as I noted at the very beginning) the principle of minimum potential energy only tells us that there is a tendency for a system to favour a configuration with the lowest potential energy (i.e. $\ce{C6H12O6}$ out of two choices we discuss). But starting from $\ce{6 CH2O}$ the natural tendency to form $\ce{C6H12O6}$ would not necessarily be realized in reality if the barrier between these configurations is relatively high in a sense already mentioned. So, it might be the case that at, say, room temperature, most of the $\ce{CH2O}$ molecules do not posses enough kinetic energy to be converted to potential energy to overcame the barrier. But you can increase the amount if kinetic energy of $\ce{CH2O}$ molecules by rising the temperature, so that the system will roll to overcome the barrier. Chemical reaction will take place, although, I'm not sure that these two ($\ce{6 CH2O}$ and $\ce{C6H12O6}$ are directly connected). But conceptually, the situation is similar to just pushing the ball from the picture above. :D