This was addressed in the (now Legends) EU novel Rebel Dawn. In short, the barrels of spice were already inside the smuggling compartments when Han and Chewie detected an Imperial ship bearing down on them. Han knows that the compartments are sufficiently shielded to pass inspection by bored customs officials but they won't stand up to the scrutiny of an Imperial 'Scanning Crew' on a mission to hunt down Spice smugglers. Han decides to dump the cargo, taking care to log its direction of travel so he can collect it later.
“Chewie, we ain’t gonna be able to outrun these Imps,” Han said. “And
we’re outgunned. But we’ve lost that guy on our tail, at least for the
moment. I think if we can get far enough ahead, we should go ahead and
dump the load just inside the Pit—the way you did that time with
Colonel Quirt on that other Run. After they’ve searched the Falcon to
their hearts content, we come back and retrieve the cargo.
and
With luck, Han should be able to track the spice’s progress and find
it again after a little searching. The barrels themselves were made of
an alloy that would show up on his sensors if he got close enough.
and
“Captain Solo, you stand in suspicion of smuggling spice from Kessel.
I am authorized to search your ship.”
Han waved at the interior. “Search away,” he said. “I got nothin’ to
hide.”
Capucot sniffed and managed to stare down his nose at Han—even though
the Imperial officer was several centimeters shorter than the
Corellian.
The captain beckoned a scanning crew into the ship. “Search every
millimeter, “he ordered. “I want that spice.”
Han shrugged and stepped aside.
The Imperials searched … and searched … and searched some more.
Han and Chewie winced as they heard crashes from the lounge and the
aft cargo compartment. “Hey!” Han protested, “I’m just an honest
trader!"
Whether these details will remain the same when the new stand-alone Solo movie comes out (or whether this incident will even be addressed) remains to be seen.