I think the reason they ask the question is threefold:
1. To see what you might be willing to accept.
2. To see if you and they are even in the same ballpark compensation-wise. If they're planning on something around $100K and you say "$175K-$185K" I think that would stop the conversation cold turkey.
3. As a standard negotiation tactic of "you go first".
When I was asked the question for my first job at my last company, I developed my answer by considering two factors:
1. The competitive market value of the job. I collected the job position description, title, responsibilities, and education and experience requirements. With that I went and looked at all the salary comparison tools (Glassdoor and Salary.com at the time, now there are more.). This gave me a pretty rough idea of what they should pay for the job.
2. How well I felt my personal skills, qualifications, educational background, etc. matched that job description. Generally job descriptions I've seen are aimed at a range of candidates - "2 year degree required, 4 year degree preferred" means if I have a 4 year degree I'm toward the upper end. If it says "3 to 5 years experience with Microsoft Excel" and I have 8 years, then I'm above the range on that item. I looked at all of the bullet items in the job description and evaluated myself objectively on each of them and then sort of took the weighted average.
In general, I think when you cite a range, the bottom number is probably what they will take as your bare minimum, and the top number is what you would really like to get paid but don't expect them to pay you. I did that and gave a range that had about a 20% difference between the two numbers. They offered me about 5% above the bottom of my range, which at the time was also a 20% pay bump from my previous job plus a much better benefit package, and I was happy to take it. (I later got a promotion, and overall about another 25% in raises and a lot more stock options.)
I have been on the hiring side of things as well. Generally when I was making hiring decisions, I would go through a similar process to the three steps I listed above for evaluating the candidate and propose what I thought was a good fair number to the HR person - I wanted a number that would be good for the person and a wise use of my company's compensation dollars. I would then usually be approved for that number plus often a few thousand dollars a year wiggle room, and maybe 500-1000 shares of options. If the candidate and I couldn't agree within that range, then I would have to either give up on the candidate or go back to HR to ask for more money. (That never happened to me when I was hiring.)
In a typical sideways move, I think there was a rough rule of thumb that we thought a candidate usually would need at least a 15% bump in salary in order to compensate them for the risk of switching jobs and to lure them away from the known into the unknown. But sometimes that rule had to be broken - maybe the other company was doing location-based compensation and the candidate lived in an HCOL area and was moving to our LCOL area, or maybe they were deliberately downshifting in responsibilities, or maybe they were retreading and switching career tracks. I don't think I ever had to deal with those scenarios personally, but we would probably have more extensive conversations with the candidate about that if we had to.