TO MAKE IT SIMPLE
There's three abilties: Ability 1, Ability 2, and Ability 3
Depending on the Pokémon's ID (this is for IVs too, but that is irrelevant to abilities)
What ability your Kakuna will get when it evolves depends on which ability number it has. For example, if Kakuna's ability number was 3, when it evolves Beedrill will have it's third ability.
Out of Kakuna's three abilities:
1 – Shed Skin
2 – Shed Skin
3 – Shed Skin
It always gets the abilty Shed Skin.
When it evolves, its ability number stays as Ability 3 (as long as you don't use an Ability Capsule), and the resulting Beedrill will have its third ability.