What directory does homebrew use to build programs?
When you install a Homebrew package, if it needs to compile it from source, it will extract the files to /tmp/package-XXXXXX and compile them there before installing to /usr/local/Cellar/package.

You can specify a different temp folder by setting an environment variable...

export HOMEBREW_TEMP=/Users/USERNAME/Desktop/temp