People buy their products because of peer pressure, conformity and sometimes obedience. People like you and me would always love to stick around with others, and embrace their thoughts so that we don't feel out of place. We want to stick to the social norm. Therefore, we all buy products by Apple and Samsung so that we are part of the majority. This is very much a social rule.
Apple's and Samsung's products may be expensive, but if you look at them both, they are called 'premium' products. They outsource their technologies, parts and components from third-party companies, so having a contract with those companies can be costly, therefore, to make profit, Apple and Samsung have to bump up the prices of their products.
Their products may be less powerful you say, but it's a marketing strategy. That is how they get so many consumers, because they just love to upgrade their gadgets without switching to an entirely different ecosystem. For example Apple, they may have "inferior" or "less powerful" products, but from the iPhone, iPhone 3G, 3GS, 4 and 5, numbers are maintaining or even growing. Why? Because Apple makes it better without radically changing the software and stuff. People want something better, faster, but at the same time, familiar.
Microsoft Windows 8 has radically changed how typical Window users have been used to, which why they received alot of flak.
HTC is not doing as well because they can't innovate anymore, as Apple, and any other tech giants have protected their own innovations with patents and trademarks. That is why other companies can no longer innovate.