The reasons why planes circle before taking off is for safety and holding patterns which help to manage traffic, weather conditions and to buy time for runway incidents or equipment malfunctions.
Air traffic controllers use holding patterns to manage the aircraft traffic, especially when an airport is busy or is also experiencing delays.
Planes may also be instructed to hold in a specific area, which is a holding pattern, to maintain separation from other aircraft and to ensure they have a safe landing sequence.
The holding patterns are not really random circles but instead are defined routes that have specific altitudes and headings.
Planes may also circle before taking off due to poor weather conditions like snow, heavy rain, strong winds or low visibility.
And circling of the plane before takeoff also allows the pilots to assess the situation and determine if take off or landing is safe or if they are trying to land they may need to divert to another airport in needed.
Planes may also circle before take off or landing if there's an obstruction on the runway such as a vehicle, another airplane, wildlife etc then they may need to circle while waiting for the runway to get clear for landing.
Also if there's a delay or any problem with the runway, the planes may be held in a holding pattern until the issue gets resolved.
And in rare cases, airplanes may circle to burn off any excess fuel or prepare for and emergency or to maintain visual separation from other airplanes, especially in a busy airspace.