The 22-year-old was out of contract at Huish Park, but the Robins will have to pay a compensation fee due to the player's age.
Bristol boss Steve Cotterill told the club's official website: "Luke is a great signing for the football club. He's another exciting young player and we've got him for an excellent fee.
"He was instrumental in the club's promotion from League One and did very well in the Championship last season, winning a raft of awards.
"He's a very versatile player and therefore offers us competition and cover in a variety of positions. We're working towards a smaller squad this season so we need that versatility."
Ayling made 185 appearances for Yeovil after joining the club in 2010.