Whether a substance dissolves or not depends on the relative forces between the solvent particles and the solute particles as well as their temperature. Dissolving gases are also highly influenced by the pressure the gas is under when encountering the solvent.
A substance only dissolves if the attraction between the particles of the solute and the solvent is enough to overcome the attraction of the particles of each substance for others of the same type. For instance, the attraction between the ions in table salt and water molecules is enough to both pull individual ions away from the salt crystal and to separate the water molecules to accommodate the ions. Benzene, on the other hand, does not attract the ions of salt strongly enough to remove them from the crystals. Napthalene does not dissolve in water because napthalene does not exert a strong enough attraction to water molecules to separate them and make room for itself.
Temperature tends to increase the solubility of solids and liquids but decrease the solubility of gases. Temperature affects solubility differently depending on the solute. Higher temperatures cause sugar to be much more soluble, but salt is only mildly affected. Gases become less soluble at higher temperatures. High pressure, on the other hand, improves the solubility of gases.