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 image menubar through Image → Fit Canvas to Selection.