Airlines pay a fee to fly over other countries. They're called overflight fees. Just as countries have rights to their land, they have rights to the air above them. Most countries "rent" that airspace to foreign airlines, allowing them to fly through it.
Some countries also provide air traffic control services. Part of the fee goes towards these services. There's no standard fee. Countries use different metrics to determine the cost. Canada's fee takes into account airplane weight and distance traveled. The United States' fee only considers the distance.
Airlines sometimes take longer flight paths to avoid high fees. But avoiding airspace is harder than it seems, because airspace can be larger than the country itself. U.S. airspace extends all the way to the Philippines. But flying over the ocean costs less than flying over land. Even though it costs less to fly over water, some airlines still have to pay the US— even if it's just going from Australia to Japan.
(Source: Business Insider YouTube channel)