The initial confusion, where the Hat briefly considers placing Harry in each of the four houses, is pretty easy to understand: Harry does embody the characteristics of all four houses, and it's hard at a glance to determine which quality is more significant (benefit of being the protagonist, I guess). But I assume your question is more about the second part, after Harry asks not to be put in Slytherin and the Hat double-checks.
With the benefit of hindsight, this seems to be a hint at the nature of Harry and Voldemort's relationship. The point of the Hat is that it determines the kind of person you are, though it's never been made clear exactly how that works1.
Whatever the method, it's not inconceivable that the Hat would have detected the connection between Harry and Voldemort. From the Hat's perspective, this is a bit of a conundrum: you have the textbook Slytherin, practically the reincarnation of Salazar, and the quintessential Gryffindor, both in the same body. What do you do when you flip a coin and it falls on its edge?
Harry solves this problem for the Hat by explicitly wanting to be in Gryffindor, but the Hat still isn't sure; what we know as Voldemort's influence would make a really good SLytherin candidate. So it does what anyone would do: it asks.
1 In fact the Hat seems to be able to determine the kind of person you are even if you don't know yourself, as in the cases of Peter Pettigrew (he had his moment of bravery at the end) and Neville Longbottom
just because he was another Weasley
. Being a Weasley might have been a part of it but possibly not the main reason