Upon reaching a certain point in the game, you get the ability to craft upgrades, which can be done in the forge in Dragon Mountain, indicated by the hammer symbol.
When crafting an upgrade, you need to combine three upgrade gems of the same rarity, which will then yield an upgrade gem of +1 rarity.
Legendary upgrades are upgrades of red rarity, which is the highest rarity in the game. In order to craft these, you need to combine either three upgrades of red or three of orange rarity.
However, there appears to be some kind of "trick" to craft a legendary upgrade using three upgrade gems of blue rarity, which I've managed to do four times so far. It looks like the names of the blue upgrade gems need to be (almost) identical in order for them to merge into a legendary upgrade, meaning they must be of the same exact type of gem, for instance: three toxic elemental gems of the same (or very similar) name.
Let me know in the comments if you've found out something more specific.