"When the apostles in Jerusalem heard that Samaria had accepted the word of God, they sent Peter and John to them. When they arrived, they prayed for them that they might receive the Holy Spirit, because the Holy Spirit had not yet come upon any of them; they had simply been baptized into the name of the Lord Jesus. Then Peter and John placed their hands on them, and they received the Holy Spirit." NIV translation
The people of Samaria believed in Jesus thanks to the teaching and example of Philip. Now, the people receive the gift of the Holy Spirit. This gives them the power to follow and to serve Jesus Christ. The same is true for us today. We can receive the same power and guidance from that same Holy Spirit.
"When Simon saw that the Spirit was given at the laying on of the apostles' hands, he offered them money and said, 'Give me also this ability so that everyone on whom I lay my hands may receive the Holy Spirit.'" NIV translation
Simon the sorcerer had money and after he saw the apostles lay their hands on people, he wanted to buy the gift like some cheap magic trick. He wanted to purchase the ability to give the Holy Spirit and did not realize that it was a gift from God.
The same sort of thing happens in many churches today but it looks a little different. There are those who are willing and able to give the local church much money but do not want to be burdened with the whole following Jesus thing. The relationship with God is not for sale.
"Peter answered: 'May your money perish with you, because you thought you could buy the gift of God with money! You have no part or share in this ministry, because your heart is not right before God. Repent of this wickedness and pray to the Lord. Perhaps he will forgive you for having such a thought in your heart. For I see that you are full of bitterness and captive to sin.'" NIV translation
Simon wanted to buy the gift of being able to lay hands on someone and have them receive the Holy Spirit but he thought that he could buy it. Peter (the apostle) rebuked him and told him to repent of his sin. His whole answer is based on looking at the state of Simon's heart. The same is true of each and every one of us. God also knows and cares about our motives (what's in our heart). We may deceive each other but cannot deceive God. Do good works but do them for the right reason which is to bring glory to God not to yourself.