What you have experienced looks very much like a "contract-to-hire" (CTH or C2H) arrangement. Company A is a contracting agency (a "body shop") that actually hires you then rents you out to their client company B. Conceivably there may be a situation where B really want you to work for them in the long run and use the CTH to serve as a probationary period for you while reducing their risks and expenses in case it doesn't work out. However, in the majority of cases the CTH is just a carrot designed to attract higher quality employees, who might be interested in working at B, to company A instead. After the contract term expires, B doesn't need you anymore, and A rents you out to company C (or dumps you, if C does not exist).
"Contract-to-hire" is not a legal term. It's just a marketing phrase intended to create an illusion that the job is somehow better than a simple temporary contract, which it isn't. The benefactor of your work (B) has absolutely no obligation to even consider hiring you once the contract term is over, while the entity you actually have a contract with, A, has every incentive to not let B hire you, lest they lose a (presumably) valuable employee and an income stream from their client.
You feel like you have wasted your time because you may have misunderstood the intent of the parties involved.