Flats help us to deal with gradients, which are variations in the light level of the background of our astrophotographs.
The ideal is an even background where no part is darker or lighter than another by a very small amount, ie. flat.
Variations from a completely flat and even background are referred to as gradients and they are generally caused by something affecting the field being photographed, street lighting, your neighbour’s security lighting, the light dome over a town, moonlight, haze, etc., and can usually be dealt with by software means, like anti-gradient software.

However, some are caused by the optics of your telescope, a mismatch of telescope optics and chip, a narrowness of the end of the telescope restricting the path of the cone of light from the primary. This is vignetting, where the middle circle of the chip appears to be more brightly illuminated than the corners, and this is actually the case.
The easiest way to deal with it is with flat frames
A flat frame is a photograph of a white evenly lit area, and what you should get of course is an evenly lit white frame, but instead, you get a frame that shows the vignetting your system suffers from, ie. It also has the central circle well lit and the corners darker. It will also have the shadows of any dust particles on the chip or its window. These show up as darker discs or circles, doughnuts.
As both the light frame and the flat frame are digital pictures you can do some mathematics and dividing the light frame by the flat cancels out the variation in the background of the light frame to give you an even background. It will also remove almost all the dust shadows too.

M31 taken with an M25C and 80ED Skywatcher complete with vignetting, dust shadows and thick haze.

This is a flat taken of the white painted square in the dome.

The final is the light divided by the flat. Vignetting and dust shadows gone.

For this to work you must obey some basic rules.

1. The flats must be taken with the telescope and camera at exactly the same settings as for the light frames, don’t change the focus, even a little, so your best plan is to take your flats immediately after your lights. Temperature isn’t important if the exposure is under 15sec or so.

2. The exposure must be long enough to register on the chip, but not long enough to saturate the pixels. Between 25 and 60% of saturation would be suitable.

3. The flat frame must be taken of a white evenly lit area, anything that will ensure the light going into the end of the telescope is white and even, to enable the chip to picks up the variations in the light caused by the optical system only.

The even white area in my case is a white painted square on the inside of the dome, lit indirectly by the dome’s interior light and for good measure I put a white T-shirt over the end of the telescope, stretched and held there with a rubber band. A friend uses a light box as used to study 35mm transparencies. Many make their own light boxes, some use morning or evening twilight, though this is more difficult as the light level changes quickly then.

Some details.
How much exposure is enough and not too much?
Check the camera maker’s website for details. You need the Full Well Capacity and the Gain
In my camera an SX M25C, the Full Well depth is 25000 e and Gain is 0.4 e/ADU
Divide the 25000 by the 0.4 to get 62500 ADU. 25% of this is 15625ADU and 60% is 37500 ADU.
An exposure of 6 or 7sec gets me an average level of 20000ADU. You’ll have to practice a bit to get a level that suits you. But don’t go for long exposures or you’ll have to consider the noise and then the temperature….
ADU? Analogue to Digital Units, a measure to indicate a level of brightness. Your acquiring software will tell you what the level is at each pixel as the mouse runs over it or as an average. In my case AstroArt shows the average at the bottom left of the screen.

You should take flats through every filter you use and in processing use the red flats with the red lights etc. In my case, a colour camera, I use AstroArt and put the flats through the same processing as the lights, which splits the lights into Red, Green, Blue and Luminance. Then in Preprocessing, I drag the flats into their box and the lights into their box and run the preprocessing step. AstroArt does it all automatically. I’m sure other software products do the same.