Why? Well, because the ring structure of the sugar molecule is lower in energy and more stable than its open chain counterpart. Now, what exactly is this reaction that takes place that allows that less stable or higher in energy open chain form to transform into the more stable, lower in energy ring structure form? Well, what this reaction is is basically a reaction between a nucleophile and an electrophile. The nuclear file in this case is an alcohol group on the sugar molecule, while the electrophile is the carbon of the carbonyl. And that can be either an aldehyde or a ketone, depending if we're looking at aldos or ketosis.