What is physically responsible for different compounds with the same types of atoms?
Okay, I'm sure there's a much more direct, down-to-earth answer that you'd find more helpful, but I'm going for something different. To stop myself from writing a book chapter, there will be some hand-waving and cut out details. With more time and thought it could be unpacked further, but hopefully I'll at least be able to give you a taste.



Right, let's forget chemistry and try to deduce the product for the following simple reaction:

$$\ce{H2 + O2 -&gt; ?}$$

To start off, we establish some basic rules:


Molecules are formed from the combination of integer amounts of atoms
Atoms cannot be formed or destroyed (i.e., the number of atoms is a conserved quantity)
There is a quantity (let's call it potential energy) inherent to each kind of free atom and molecule
The Universe tends towards the lowest possible potential energy


With nothing else to guide us, we are forced to consider every possible product (isomers neglected for simplicity):

$$\ce{H2 + O2 -&gt; H + O + H_2 + HO + O_2 + H_3 + H_2O + HO_2 + O3 +  H4 +...}$$

Now, if the point is to minimize the total potential energy of the system, we can develop a heuristic (which will turn out to be wrong, but entertain the idea for now). First, find the specific atom or molecule among the listed products with the lowest possible energy,$^1$ then make as much of it as allowed by stoichiometry. When no further can be made, look for the next lowest energy molecule, then make as much of that as possible, and iterate the process until no lower energy molecules can be made.

That's nice and all, but without knowing which molecules have lower or higher energy, you don't get anywhere. Here, we either resort to some hefty quantum mechanical calculations, or empirical data. By doing some thermochemical experiments (measuring enthalpies of formation), it turns out that $\ce{H_2O}$ is the molecule with the lowest energy of the list by quite a margin, so it's going to be the main product. Indeed that is what is observed empirically when hydrogen is burnt in oxygen.

So we know that water is the lowest energy product for the reaction, but what happens if we mix $\ce{H2}$ and $\ce{O2}$ with a ratio different from that in water? Say you have two moles of $\ce{H2}$ and two moles of $\ce{O2}$, does that mean you now get $\ce{H2O2}$ or potentially some other products? In fact, this is not the case. The heuristic above still applies to minimize the potential energy of the system, so $\ce{H_2O}$ is preferentially produced. This shouldn't come as too much of a surprise, because the low energy of $\ce{H_2O}$ is an inherent feature of the molecule, and is entirely independent of how much $\ce{H2}$ and $\ce{O2}$ you decide to mix in an experiment.

Then what happens to the excess $\ce{O2}$? Again following the heuristic, it will seek out the lowest energy molecule available. Since there's no more hydrogen left to react, it has a more limited number of possibilities. It could conceivably still react and turn into other molecules (for example, $\ce{O3}$ is perfectly acceptable), but it turns out that of the remaining candidates, $\ce{O2}$ is the molecule with the lowest energy, so the reaction just stops there, and we get a mixture of $\ce{H_2O}$ and left over $\ce{O2}$.

Okay, we've made some progress, but we need to talk about some important complications. In the real world, at any temperature above absolute zero, you can actually retain a small amount of molecules with a higher potential energy. All of those lower energy molecules can randomly gain a bit of thermal energy, and every so often that causes them to react and turn into a higher energy molecule. If the lowest energy molecules are much lower in energy than anything else (as is the case for $\ce{H_2O}$ among the combustion products of $\ce{H2}$ and $\ce{O2}$), then this effect is very small and can be neglected. However, in many cases there isn't such a clear-cut winner, and you end up getting a mixture of different products with similar energies. This is especially true as you get into more complicated chemistry.

Unfortunately, even after all this we're still missing a very big piece of the puzzle. Everything I've mentioned so far is a part of thermodynamics, and that alone cannot always predict the product of a reaction. Allow me to prove it. According to everything I've argued, $\ce{H2}$ and $\ce{O2}$ will always react to form primarily $\ce{H2O}$ since it has the lowest energy, and $\ce{H2O2}$ can never be a significant product. But hydrogen peroxide is made in very large amounts industrially, so how do they do it? A quick search on the internet provides the answer, the anthraquinone process, whose net reaction is simply:

$$\ce{H2 + O2 -&gt; H2O2}$$

What gives? What we've been missing is the concept of kinetics. For many transformations in nature, chemical or otherwise, even if the final state has a lower energy than the initial state, they are often separated by an energy barrier. Even if there can be a net release of energy, the transformation is hindered because to start off it needs a net input of energy. 

This is the case even for the reaction $\ce{H2 + O2 -&gt; H2O}$. If you just mix the gasses together at room temperature, they will not react even after a century. Either some initial energy needs to be supplied (a flame, a spark, etc.) or the energy barrier needs to be decreased by using a catalyst (Pt), after which the reaction will proceed in a fraction of a second.

When you consider all possible reactions, and all the energy barriers separating the different molecules, you get the very high-dimensional potential energy surfaces I mentioned in my comment. All our work in Chemistry boils down to finding reliable paths in (or making our own by actively modifying) the crazily complex energy landscapes and exploiting them for our purposes.

$^1$ Technically, the lowest amount of potential energy per amount of atoms the molecule is made of. You can think of it as the lowest potential energy per gram of molecules.



Here's something which might also help. Is there a case where all this potential energy and kinetic stuff doesn't skew the results, and you can tease out the purely statistical effect? Sure, let me present a typical case.

Consider the following transesterification reaction (poorly drawn because I don't currently have access to ChemDraw):



Both sides of the equation have an alcohol and an ester. The alcohols are very similar to each other, and the same can be said for the esters. Because of this, there's barely any difference between the potential energy of the products and the reagents. 

Now consider taking a mole of the starting  ester and alcohol. In the beginning, there is only ethanol and no methanol, so statistically you'd expect the reaction to happen in the direction shown. As the reaction progresses, the amount of ethanol available is reduced, disfavouring further generation of products, and the presence of methanol now actually allows the reaction to also go the other way around. By pure statistics, you'd expect the reaction to stop when there's about the same amount of methanol and ethanol, half a mole each, and this is what is observed. In short, simple transesterification reactions tend to have equilibrium constants very close to 1 (e.g. this article).