There is no in universe explanation form the cartoon continuities.
For the comics, it look like there is one.
Form the Transformers Wiki, Note section of the Trailer article
When Optimus Prime transforms into robot mode, his trailer usually disappears, only to reattach itself to Prime whenever he goes into vehicle mode.
Many fan theories about this disappearance have arisen over the years, with the most popular being that Prime stores the trailer in subspace.
IDW comics gives a nod to this, by having the trailer arrive at Prime's location using the orbital jump .
scifi.stackexchange.com/users/45/DavRob60
The General consensus is that it just goes over there, i.e. just out of camera shot.
Funnily this is subverted in the original Marvel Comics four issue run, where Prime's trailer just sits there clearly visible not doing anything while the action takes place around it.
The second bullet point on this tfwiki page says: During the entire battle at the Witwicky's house, Optimus Prime's trailer sits parked, frequently in view.
This is in stark contrast to the more fantastical cartoon, where it would just roll off-screen and vanish.
scifi.stackexchange.com/users/1223/Rigas
There is now an official answer, the: Subspace Storage Pocket Greg Sepelak and Trent Troop used this phrase in two of their stories for the official Transformers Collectors' Club comic.
Hence this phrase is now canon.
The subspace storage pocket is used by multiple transformers, eg for weapons storage, gestalt parts and mass changes .
Source; this rather awesome video 1 : 1
The whole YouTube channel is great.
scifi.stackexchange.com/users/143/Reinstate Monica - Goodbye SE
Another theory produced by a Transformers fan is that the trailer is created each time by the creation matrix .
To quote from the end of the video : I think the matrix gives Optimus Prime and Rodimus Prime the ability to manifest a trailer whenever they transform into a truck.
Personally I'm not convinced that the creation matrix has the ability to manifest real world objects; I think its purpose is to impart life to Transformers.
But I like the theory and the fact
that the fan has, rightly, also taken Rodimus Prime into account.
scifi.stackexchange.com/users/143/Reinstate Monica - Goodbye SE
Optimus Prime has people to take care of these things Here
we see his loyal subordinate Huffer towing his trailer:
scifi.stackexchange.com/users/30449/EleventhDoctor
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 dimension and can bring it to this dimension 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.
scifi.stackexchange.com/users/62450/Vince
When (the original, G1) Optimus Prime transformed into robot mode, this trailer just seemed to disappear. When he went back into truck mode, there was his trailer again!  I'm guessing this was to help the animation and story telling. But what was the  in-universe  explanation? Where did it go?  This question is applicable to both the comic and the cartoon continuities.
What happened to Optimus Prime's trailer when he transformed into a robot?
scifi.stackexchange.com
<transformers><transformation>
scifi.stackexchange.com/questions/4659
scifi.stackexchange.com/users/143/Reinstate Monica - Goodbye SE
Many  fan theories  about this disappearance have arisen over the years, with the most popular being that  Prime stores the trailer in subspace. The  subspace storage pocket  is used by multiple transformers. However, another theory is that Optimus stores his trailer in another dimension and can bring it to this dimension at will -either because any transformer can or because the matrix allows him.  Greg Sepelak  and  Trent Troop have confirmed that transformers use Sunspace Storage Pockets to store things they do not need when they transform. This was once regarded as a theory, but is now canon due to its confirmation during two of Greg Sepelak and Trent Troop's stories.
 Greg Sepelak  and  Trent Troop have confirmed that transformers use Sunspace Storage Pockets to store things they do not need when they transform. This was once regarded as a theory, but is now canon due to its confirmation during two of Greg Sepelak and Trent Troop's stories.