How to detect if anyone stepped on block and then execute command toward that play

/execute @a ~ ~ ~ detect ~ ~-1 ~ minecraft:stonebrick 1 /tp @p @e{type=Villager]

When ANY player steps on mossy stonebrick (minecraft:stonebirck 1) it teleports the nearest player to the command block to the villager regardless if he step on the mossy cobble.

The /execute command modifies the sender of the command to that of its target. Any subsequent selectors in commands being run are going to use the executor as the sender.

This comes into play with sender bias, which forces a target selector to target the entity that ran the command. The command you've presented already uses it. The executing player will be the one to be teleported, so long as they are alive.

The @a selector is the only target selector capable of targeting dead players. If a player dies while on top of stonebrick, they will teleport the nearest living player to them rather than themselves, because @p cannot target dead players.

The fix for that in particular is to use the @a selector while reducing the count (via c parameter) to 1, which also enforces a sender bias. The command itself is still functional as you need it without having to have changed anything else.

/execute @a ~ ~ ~ detect ~ ~-1 ~ minecraft:stonebrick 1 /tp @a[c=1] @e[type=Villager,c=1]

/execute is used to change the sender and origin of commands to run. /tp is being run through the player, not by the command block. @p will be in reference to the executing player's location, and due to sender bias, @p will always target that player so long as that player is alive. Your command was originally working fine apart from the dead targeting flaw.