With a Reach weapon (glaive, lance, pike, halberd) you can do it with a Maneuver
The Battlemaster Fighter can use a number of combat maneuvers, one of which has the desired effect if we are wielding a reach weapon:
Sweeping Attack
When you hit a creature with a melee weapon attack, you can expend one superiority die to attempt to damage another creature with the same attack. Choose another creature within 5 feet of the original target and within your reach. If the original attack roll would hit the second creature, it takes damage equal to the number you roll on your superiority die. The damage is of the same type dealt by the original attack.
It doesn't work with a spear because a spear does not have the Reach property, but one of the other polearms with Reach could do this using the Sweeping Attack maneuver.
If you don't want to play a Battlemaster specifically, you can obtain the maneuver via the optional (ask your DM) fighting style "Superior Technique", found in Tasha's Cauldron of Everything:
Superior Technique
You learn one maneuver of your choice from among those available to the Battle Master archetype. [...]
You gain one superiority die, which is a d6 (this die is added to any superiority dice you have from another source). [...]
Or through the feat Martial Adept:
- You learn two maneuvers of your choice from among those available to the Battle Master archetype in the fighter class. [...]
- You gain one superiority die, which is a d6 (this die is added to any superiority dice you have from another source). [...]
The Superior Technique feature requires you to still be a fighter, just not a Battlemaster specifically, but the Martial Adept feat can be selected by anyone. The disadvantage with these options is that the superiority die gained is only a d6 and does not scale up, for that, you have to be a Battlemaster.
You can also technically take the Superior Technique fighting style via the Fighting Initiate feat, but that would be a weaker version of the Martial Adept feat, and there would really be no reason to do that, unless you already had taken the Martial Adept feat and did not select Superior Technique for either of the fighting style options granted.
Polearm Master Bonus Action attack, again, with Reach weapon
The Polearm Master feat grants a Bonus Action attack when using a polearm:
When you take the Attack action and attack with only a glaive, halberd, quarterstaff, or spear, you can use a bonus action to make a melee attack with the opposite end of the weapon. This attack uses the same ability modifier as the primary attack. The weapon’s damage die for this attack is a d4, and it deals bludgeoning damage.
If the weapon used has the Reach property, this Bonus Action attack can be used to hit a creature immediately behind the primary target, but the second target will have half cover - the rules for cover state:
A target with half cover has a +2 bonus to AC and Dexterity saving throws. A target has half cover if an obstacle blocks at least half of its body. The obstacle might be a low wall, a large piece of furniture, a narrow tree trunk, or a creature, whether that creature is an enemy or a friend.
Being a bugbear allows you to use the non-Reach spear.
The bugbear has a racial trait that can circumvent the requirement of using a Reach weapon:
Long-Limbed
When you make a melee attack on your turn, your reach for it is 5 feet greater than normal.
As a bugbear, you could make the Polearm Master Bonus Action attack against a second target ten feet away with a spear, despite the spear not having the Reach property.
However, you have to select your targets carefully with the Sweeping Attack maneuver. The Long Limbed trait only increases your reach for the particular melee attack you are making, and Sweeping Attack damage is not a melee attack, so the Long Limbed trait would not apply to "your reach" mentioned in the maneuver description. So to use Sweeping Attack as a bugbear without a Reach weapon, you must make the melee attack against the further target, then use the Sweeping Attack maneuver against the nearer target.
For weaker enemies, the DM may permit the "Cleaving through Creatures" rule.
Again, with a Reach weapon. The optional Cleaving through Creatures rule is pitched as an optional rule for dealing with large numbers of low hit point enemies, found in the Combat Options section of the Dungeon Master's Guide:
When a melee attack reduces an undamaged creature to 0 hit points, any excess damage from that attack might carry over to another creature nearby. The attacker targets another creature within reach and, if the original attack roll can hit it, applies any remaining damage to it. If that creature was undamaged and is likewise reduced to 0 hit points, repeat this process, carrying over the remaining damage until there are no valid targets, or until the damage carried over fails to reduce an undamaged creature to 0 hit points.
If you have a Reach weapon and one-hit-kill an immediately adjacent target, using this rule, any excess damage can be applied to a secondary target behind the first.