In The Trial, Joseph K. is arrested for reasons unbeknownst to him. Throughout the novel, he tries unsuccessfully to understand the courts and their rationale. Courts and legal systems are supposedly based upon logic and fairness. But K. can not determine how they are being logical or fair.
Surrealism was a movement which was opposed or at least deviated from the logic and rationalism of the 18th and 19th centuries (the Enlightenment and Industrial Revolution in particular). Surrealists sought to blend the real and the imaginary, or the conscious and the subconscious. Reacting to the so called rational thought of their era (and reacting to the lack of logic and fairness in the wake of World War I), the surrealists entertained the idea of tapping into the unconscious: as a way of getting past the limits of conscious, rational thought. So, as much as the Surrealists were skeptical of the logic of cultural institutions (including art), K. became skeptical and disillusioned by those same institutions. He could not comprehend their actions even though they were supposedly based on logic. Consequently, he would hallucinate (as the flogging in the junk room seems to be), taking inner (perhaps subconscious) images and projecting them externally, thus blending the real and the imaginary, the conscious and the subconscious. He did this as a way to make sense of things; even if such images only complicated his frustration.
If there is no sense to the order of cultural institutions (the legal system in The Trial or the logic of countries who resort to a World War), then deviations to the subconscious are either equally illogical, a useful alternative, or in psychoanalytic study: a way to get underneath all of the conscious logic of those institutions.
Not comprehending his arrest and the logic of the High Court, K. is left feeling isolated. Hence, he retreats into his own mind and occasionally daydreams. (Again, one of the tools of Surrealism was tapping into the subconscious.)
Another way to think of this is that K.'s experience in the real world was surreal to him because things did not make sense. The real world of his trial seemed, to him, as random and ambiguous as some dreams can be. There are times in the novel where K. is equally bewildered by what may be real and/or imaginary. The novel is about trying to make sense of the real world, through direct observation or by subconscious tangents. The imagery at the end of Chapter 9 sums these ideas up. K. is being led by a priest, an authority figure, someone presumably with answers, but is simply leading him through darkness. This scene also blends the real and the imaginary:
In silence, they carried on walking for some time, K. stayed close beside the priest without knowing where he was. The lamp in his hand had long since gone out. Once, just in front of him, he thought he could see the statue of a saint by the glitter of the silver on it, although it quickly disappeared back into the darkness.
K. thought he could see a saint, maybe a clue, something to give sense and context, but the clue disappears. Likewise, the Surrealists tried to create works which blended real life and dreams. When the real world does not make sense, why not impose images and ideas from dreams to try and make sense of them. In K.'s case, the search for sense is endless.