When cheese melts, the protein molecules break apart and transform into liquid, which moves through the water and thickens it. That's why a low and slow cook is crucial.
Melt butter in a heated saucepan and mix in flour and heavy cream, whisk until smooth, add grated or shredded cheese, reduce the heat to medium-low, and stir until it's melted.