They have a database of games, according to shots taken, where on the field players have had the ball, or a penalty kick being awarded will greatly raise it. It's supposed to average out where these data points or ball possessions result in goals...
I don't know. Some people swear by it, I still struggle to analyze it and really understand it. I've watched games where I don't find the xg to be accurate but for the most part it is good. However I've found when these tools are used to predict the future they're unreliable.
I can remember Brazil being forecasted as a 70% favorite or so when Germany destroyed them in the 2014 semi's. I don't know anyone who thought that team minus Neymar was that much of an overwhelming favorite in that game. I do usually pull for Germany due to ancestry and the Americans being [Poor language removed], so i could have had my own bias as well. But that just always stuck with me as something that was way off, and it was shown to be.