How does countering spells work in stack?
You can target any spell on the stack, there is no rule limiting it to only the top item on the stack. As you can see below the term counter doesn't have anything that effects how targets have to be chosen, and the spells don't say they have to target the top most object on the stack, so they are able to target any spell that is on the stack.

114.1. Some spells and abilities require their controller to choose one or more targets for them. The targets are object(s), player(s), and/or zone(s) the spell or ability will affect. These targets are declared as part of the process of putting the spell or ability on the stack. The targets can’t be changed except by another spell or ability that explicitly says it can do so.
701.5. Counter
701.5a To counter a spell or ability means to cancel it, removing it from the stack. It doesn’t resolve and none of its effects occur. A countered spell is put into its owner’s graveyard.
701.5b The player who cast a countered spell or activated a countered ability doesn’t get a “refund” of any costs that were paid.