When you lift heavy deadlifts and squats, your whole body is under high compressive force. The muscles squeeze your blood vessels, which increases the hydrostatic pressure of your circulatory system. In other words, your blood pressures spikes up during the lift. The body attempts to restore homeostasis during the lift by engaging physiological responses which lower the blood pressure - mostly by releasing vasodilators, which are chemical signals telling the smooth muscles lining your blood vessels to get relax. When you finish the rep, your muscles relax, and the pressure drops suddenly - but the vasodilation response has been activated, and your blood pressures spikes down for a few seconds before the body can act again to restore equilibrium.
It's normal - nothing to be alarmed about. All serious lifters have experienced this, including to myself. However, this physiological effect can lead to passing out, and passing out can be dangerous because you can take a nasty fall. It's also a reason to squat over a rack. Even if you think you could dump the weight if things got out of hand, you might pass out at the top of the rep one day, and then the only thing that's going to save you from a squashing is the rack.
To mitigate this effect, work on your breathing. Having good breathing will give your whole body, including your brain, more oxygen. It won't stop the blood pressure spikes, but it will allow you to stay conscious at a lower blood pressure. Exhale strong and long after you pass the sticking point on the way up from the rep. (Say at the top 25% of the ROM). At the top, after you finish a full exhale, and while keeping your core tight and engaged, breathe deep into your chest and upper belly (diaphragm). If your vision is clear and your head is solid, take the rep. Otherwise, go through one more deep breathe cycle - without ever losing tension in your stomach. This is hard and takes practice, but you can do it. (It's the same kind of breathing for singing, by the way.)
Note that lightheadedness during a rep, or prolonged lightheadedness after a set, cannot be explained by this mechanism. Only brief lightheadedness at the top of a rep or after the set is explained by this physiological response.