When a person has sinned, in particular sexually immoral sins, do they repent only to God? Is it necessary to repent to a person, such as a preacher or elder?
The purpose of confession is to face the fact that we sinned -- that we are not going to hide from that fact. Unacknowledged sin destroys us internally. "When I kept silent, my bones grew old through my groaning all the day long. For day and night Your hand was heavy upon me; my vitality was turned into the drought of summer. I acknowledged my sin to You, and my iniquity I have not hidden. I said, "I will confess my transgressions to the LORD," and You forgave the iniquity of my sin." (Psalms 32:3-5). Confession is made to the ones we harmed by our sins. Since all sins are against God, the first and most important confession we need to make is to Him.
"This is the message which we have heard from him and announce to you, that God is light, and in him is no darkness at all. If we say that we have fellowship with him and walk in the darkness, we lie, and don't tell the truth. But if we walk in the light, as he is in the light, we have fellowship with one another, and the blood of Jesus Christ, his Son, cleanses us from all sin. If we say that we have no sin, we deceive ourselves, and the truth is not in us. If we confess our sins, he is faithful and righteous to forgive us the sins, and to cleanse us from all unrighteousness. If we say that we haven't sinned, we make him a liar, and his word is not in us" (I John 1:5-10).
Often the one in sin realizes how badly his sin affected his relationship with God and so he desires the support of his fellow Christians in approaching God. You can see this with Simon.
"Now when Simon saw that the Holy Spirit was given through the laying on of the apostles' hands, he offered them money, saying, "Give me also this power, that whoever I lay my hands on may receive the Holy Spirit." But Peter said to him, "May your silver perish with you, because you thought you could obtain the gift of God with money! You have neither part nor lot in this matter, for your heart isn't right before God. Repent therefore of this, your wickedness, and ask God if perhaps the thought of your heart may be forgiven you. For I see that you are in the gall of bitterness and in the bondage of iniquity." Simon answered, "Pray for me to the Lord, that none of the things which you have spoken happen to me"" (Acts 8:18-24).
This is why we read in James: "Confess your trespasses to one another, and pray for one another, that you may be healed. The effective, fervent prayer of a righteous man avails much" (James 5:16). That is why we allow opportunities for people to come before the congregation. It is so others can pray on the behalf of the sinner that God will forgive them. Going before a congregation is not required. Nor does God say that confession has to be to a preacher or elder. The talking to fellow Christians about your problems is an option to get yourself straighten out quicker than you might on your own.
We realize that our actions impact other people. When our actions harm someone else, we should approach them to admit our wrong and ask for their forgiveness. The goal always is to get a person to turn from their sins. Once the person repents, the matter is dropped to minimize the embarrassment caused by sin. That is why you see in Matthew 18:15-17 that a problem between two Christians is not immediately brought before the church. Attempts are made to work out the problem with the minimal number of people involved as possible. It is only when the matter is not resolved that it is eventually taken before the church.