What does $temp do?

What any variables used in samples do depends entirely on that specific sample. If a sample assigns a query to a variable called $temp, then that’s what $temp does. It doesn’t mean anything else, it’s just what’s shown in the sample.

Guide and tutorial writers can and will create and use any variables they want. You will need to read and understand the guide to understand what they’re doing, or ask the author if you’re unsure. If you need to, learn basic PHP first, as it will help a great deal.

That being said, what you’ve described — modifying $wp_query directly and then resetting it — is something you should absolutely never do. Do not take further advice from any source that told you to do this.

If you need to query posts for your own purposes, use new WP_Query() or get_posts(), and if you need to modify the main query, use the pre_get_posts filter. These are thoroughly documented across the web, and this site, if you need to know more.