The ability is determined by the Pokemon's last bit of the PID, which is a 32-bit number. Namely, if it is zero, the Pokemon will have the first ability, while if it is one, it will have the second one. Alternatively, this can be understood in the following way: the Pokemon's ability depends on whether the PID is even or odd. Of course, if the Pokemon can have only one ability, then it will have that ability irrespective of what the PID is. However, if a Pokemon had only one ability in ADV and has two possible abilities in DP and is transferred from Ruby (say) to Diamond and evolved, it can change its ability depending on what its PID is. For example, if a Limber Meowth is transferred from Fire Red to Diamond and is evolved in Diamond, it can change its ability to Technician if its PID is odd.
That said, we need a chart telling us which ability bit corresponds to 0 or 1, so that we know how the abilities change during evolution.
Fortunately, yamipoli provided me with such a chart last Summer. At that time I was researching on how to know if an ADV Pokemon changes its ability or not when transferred to DP and evolves, and she had given me that chart very generously.
All Pokemon having the same ability/abilities in their evolution line don't change their ability. For example, a Keen Eye Pidgey will evolve into a Keen Eye Pidgeotto which evolves into a Keen Eye Pidgeot; it will never change its ability to Tangled Feet upon evolution. NOTE: IT SEEMS LIKE POKEMON THAT ARE TRANSFERRED FROM XD CAN VIOLATE THIS RULE THOUGH!
The others change as follows:
Venonat -> Venomoth
Compoundeyes -> Shield Dust
Tinted Lens -> Tinted Lens
Meowth -> Persian
Pick Up -> Limber
Technician -> Technician
Tyrogue -> Hitmonlee
Guts -> Limber
Steadfast -> Reckless
Tyrogue -> Hitmonchan
Guts -> Keen Eye
Steadfast -> Iron Fist
Tyrogue -> Hitmontop
Guts -> Intimidate
Steadfast -> Technician
Rhyhorn -> Rhydon -> Rhyperior
Lightningrod -> Lightningrod -> Lightningrod
Rock Head -> Rock Head -> Solid Rock
Porygon -> Porygon2 -> Porygon-Z
Trace -> Trace -> Adaptability
Download -> Download -> Download
Aipom -> Ambipom
Run Away -> Technician
Pick Up -> Pick Up
Yanma -> Yanmega
Speed Boost -> Speed Boost
Compoundeyes -> Tinted Lens
Snubbull -> Granbull
Intimidate -> Intimidate
Run Away -> Quick Feet
Teddiursa -> Ursaring
Pick Up -> Guts
Quick Feet -> Quick Feet
Remoraid -> Octillery
Hustle -> Suction Cups
Sniper -> Sniper
Poochyena -> Mightyena
Run Away -> Intimidate
Quick Feet -> Quick Feet
Numel -> Camerupt
Oblivious -> Magma Armor
Simple -> Solid Rock
Buneary -> Lopunny
Run Away -> Cute Charm
Klutz -> Klutz
Glameow -> Purugly
Limber -> Thick Fat
Own Tempo -> Own Tempo