The Fit Canvas to Selection command adapts the canvas size to the size of the selection, in both width and height.
You can access this command from the main menu through Image → Fit Canvas to Selection.