The role of mergers and gas accretion in black hole growth and galaxy evolution