CALGARY, Alberta—Canadian pipeline operator Enbridge Inc. on Tuesday agreed to buy Houston’s Spectra Energy Corp. in an all-stock deal valued at about $28 billion, creating a North American energy-infrastructure giant at a time when growth is challenged by lower commodity prices and higher regulatory hurdles.

The deal is a shift away from reliance on crude oil and is a bullish bet on natural gas by crude-hauling heavyweight Enbridge. The Calgary-based company expanded existing cross-border pipelines in recent years to help...