Why does a lack of pipelines cause Canadian crude oil to trade at a discount compared to US crude oil? There is an ongoing political battle in Canada about the proposed expansion of the Kinder Morgan Trans Mountain pipeline.

One argument in favour of the expansion that is repeated by just about every news article on the topic (example) is that the lack of pipeline capacity to get Canadian oil to market is causing Canadian oil to sell at a discount compared to US oil.

I'd like to understand this connection between pipeline capacity and oil price.

Naively, I would think that if pipeline capacity is scarce and the oil has to be transported by more expensive means like rail, its price would be higher to reflect the increased cost.
If you take the oil price as a given, meaning not set by Canada but by the market, then the costs of more expensive transport will eat the profit margins. For example, if the price of the oil is 100 USD per barrel, 20 USD would be costs for Canada, while for US producers the cost is, for instance, only 10$.

Keep in mind that a project investment analysis should take in account the cost of the pipeline divided by the number of years that it will be used to deliver that oil, adjusted for potential changes in oil demand, and including costs of insurance and removal at the end on the life of the pipeline. When all is taken in account, it might be that the oil will always be sold at discount when coming from Canada, and building the pipeline is not such a good investment after all - but only a full analysis would tell.