Henry Higgins, a linguist, happens to overhear Cockney flower seller Eliza Doolittle speaking in lower class dialect at Covent Garden. He brags that:
in three months I could pass that girl off as a duchess at an ambassador's garden party.
He goes home, thinking no more of that passing comment or of the Cockney flower seller.
In her cold room, however, Eliza ponders what Higgins said about teaching her to speak like lady. She'd like a better life for herself, so she boldly shows up at his doorstep the next day. She demands lessons and offers to pay a shilling apiece for them.
Higgins is ready to throw her out when Colonel Pickering, who happens to be there, offers to pay. He also holds Higgins's feet to the fire, betting him that he won't be able to pass Eliza off as a duchess. Higgins rises to the challenge and agrees to the bet. In this way, Eliza ends up being educated to act, dress and speak like a lady.
This has nothing to do with Higgins having any compassion for Eliza. He plans to throw her out again once the bet is over.