The starch in the flour stabilizes the melting cheeses which prevents it from breaking or turning stringy or gritty.  It also is the base of the béchamel which thickens the sauce so it’s rich and creamy.