Excess capital gain real state

If I make over $500.000 in capital gains from a sale of a primary residency filling jointly with my spouse. 

Can I invest the excess in a new property ? Will I pay capital gain taxes on the excess?
What happens in invest the gains in a two properties, a primary residence and a rental property? Will I have to pay taxes on the excess?