There are two processes involved in what you are describing. The first has to do with which candidate routes actually make it into the routing table. This is determined using the AD.
Then we go to the second process, which is choosing the route among all possibilities already in the routing table. Once routes enter the routing table, the next criterion is the longest prefix length. The more specific entry in the routing table is chosen.
If you have two or more routes to the specific destination of the same prefix length, then the metric is used to determine the best path.
If the metric is still the same, then load balancing may take place depending upon the configuration.