The ark was taken to Kiriath Jearim. So for twenty years the ark remained in obscurity. There are times when the church seems to go into decline in a nation, but even in those times the presence of God is not gone completely. In some corner, in some place, there will be some who still worship the Lord, and a time will come when the church will rise again.
So, eventually the people as a whole did turn back to the Lord. Samuel was the prophet to the nation and instructed the people on what they needed to do. This included ridding themselves of idols and devoting themselves to God. When they did this they would experience deliverance from their enemies. The same applies to all of us. The first steps to take to gain freedom is to repent and turn to God. Then we will find that our problems start to get sorted out. And the people obeyed God.
The people fasted, prayed and repented, and Samuel prayed to the Lord for them. The Philistines so the gathering of the people as an opportunity to attack them. The people were afraid, but they implored Samuel to keep crying out to God for them. This he did, along with offering sacrifices.
The Philistines came to attack, but the Lord intervened directly to save His people.
So the people were protected from the Philistines, and Samuel continued his role as leader of the people.
See how differently things turned out here from the defeat a couple of chapters ago. What was the difference? Obedience to the Lord. The most important decision we make in life is the decision to serve the Lord.