Theory 1: inside every transformer is the ability to shrink--but not enlarge--themselves and any of their components in robot or disguise mode (This would also explain why Sky Fire and Astro Train can fit groups of transformers who shouldn't fit inside them--and why Megatron and Sound Wave transform into things much smaller than their robot modes.).
Theory 2: Optimus stores his trailer in another demension and can bring it to this demension at will--either because any transformer can or because the matrix allows him.
Theory 3: like the Allspark in the first Michael Bay film, Transformers can fold themselves and different components over into very small objects; in Optimus's case, he would store the condensed trailer in his body.
Theory 4: like Mirage, Optimus's trailer can turn invisible and follows Optimus around everywhere while he's in robot mode.
Theory 5: (insert your own because there are countless ways to explain the topic's question when you're talking about living robots who can transform into vehicles and have technology thousands or even millions of years beyond current technology, and I don't feel like thinking of any more.)