How does a Pokémon gain CP when evolving?

Solution 1:

When you evolve a Pokemon, the CP scales to the same percentage of your max at that level.

You can tell the approximate percentage by looking at the circle above a captured Pokemon. After evolution, the marker will always be in the same place.

For example, if you're evolving a Pokemon with a Max CP of 100 to a Pokemon with a Max CP of 300, your current CP will triple to keep the same ratio.

Solution 2:

When evolving a Pokemon, a random multipler is applied to it's original CP to produce it's resulting evolved CP. This multiplier has specific upper and lower bounds for each Pokemon and upon evolution the multipler being applied will being randomly chosen from with these bounds. So evolving two identical Pokemon with the same CP can result with different evolved values.

Here is a spreadsheet containing all the evolution multiplers for all Pokemon in the game. This spreadsheet was curated by a Reddit user so credit goes to him for the great work. This data is based off of statistical results and submitting more data will help round out the values making it more accurate.