It's hard to find any solid canonic answer to things like this, but let's take a look at it from an analytical standpoint.
There are three possible answers:
First option : Someone had given the order, or flipped a switch, to make the Garbage Compactor do its thing (hopefully with the chance of killing the people in it.)
This seems like the most plausible thing at first. The heroes are being chased, they jump down a vent and find themselves in the trash compactor, and the villains of the movie activate them to worsen their peril.
But after looking at the scene a bit, it seems highly unlikely, the biggest con being that Imperial ships, and even the death star, seems to have no to low surveillance, so no one could really validate that they were down there.
Alas, Han Solo actually gives us the best clue in his quote : "It's not gonna' take them long to figure out what happened to us." clearly meaning that they escaped down the garbage chute.
And given the squad is right on their heels, and a big blasted hole can be seen in the corridor, it would not be unlikely that one trooper reported "Sir, they fled down the garbage chute", and another went "activate all garbage compactors, that should take care of them".
But in fairness, let's look at the other possible options:
Second option : The garbage compactor just compacts within a certain time interval, and was doing so here.
Now this is a horrible impractical invention if it's true. Having compactors set on an interval, would make them cost power when no trash is even there, and if they get overflooded, there would be nothing to do but wait to the next cycle. Then again, the garbage compactors are rather inefficient due to other things, like :
- Would not vents leading into any garbage-disposal system allow the fetid smell of rotting garbage, spores, molds, etc., to seep up into the rest of the Death Star?
- Would not it have been more prudent for the designers of the Death Star to opt for a closed system, like a septic tank?
- Why do both walls of the trash compactor move towards each other, rather than employing a one-movable-wall system that would thus rely on the anchored stability, to say nothing of the strength, of the other, non-moving wall, to crush trash more effectively?
So since there are no clues in the movies about this system, we can only say that it's highly unlikely. Given that the existing waste has mold, rust, heck even a native creature living there, its clear that these intervals would have to be wide apart. Having said that, Han Solo and friends are having a string of bad luck, so yeah...
Third option : The garbage compactor starts it's process when it's reached a certain amount of trash, or detected any specific elements.
This would be the logical way of building a deep space garbage disposal unit, BUT there is still plenty of room in the garbage compactor when it starts. Some say that it might be triggered by weight, and our four heroes triggered some weightplate, but that would make no sense to build a garbage sensory system on this.
A theory is that the garbage room detected lifesigns, and to get rid of the Dianoga that plague the trash systems. But if this is true, it's clearly super ineffective.
Lastly, some say that the process is triggered by the blaster rifle being fired at the door. Personally, I don't understand why this sensor would be installed (and even less who installs a door in the bottom of a garbage compactor), and the room doesn't really seem to "care" that the laser beam bounces around.
Fun fact : Did you know that the texture of the garbage walls were recycled as internal wall texture for the WALL-A robots in the movie Wall-E ? Well now you do.