A substance dissolves in another because the second substance is able to dissolve the bonds between the molecules of the first. Dissolving a substance and breaking these bonds requires energy, which is why stirring sugar in a glass of water makes the sugar dissolve. The stirring puts energy into the system and causes the bonds to break down.
In general, molecules and their components will arrange themselves into the easiest configuration that requires the least amount of energy to maintain in any given environment. The strength of the bonds also differs depending on the particular substance, which is why some substances are harder to dissolve or do not dissolve at all.