I thought that there was a routing algorithm that was supposed to optimize this?
BGP by default prefers the path going through the least number of autonomous systems. A carrier will announce the networks they can reach. Each carrier handles routing within their autonomous system separately, with other routing protocols. BGP is concerned with routing between autonomous systems, not with routing inside each system. It is common for BGP carrier implementations to set certain paths as preferred based on contract terms with other carriers, i.e. cost.
Other routing protocols work differently. Some of them by default prefer paths with the most bandwidth. Latency is usually correlated with hop count and bandwidth. The devil is in the details.