This is simply because of how batteries work. We're focusing on lithium ion batteries, the most common in computing at our current point in time, and these are simplifications and not electrical engineering down to the exactest detail.
They can only hold the max charge when brand new. As they are used (charged and discharged), literal physical wear is happening within the battery (really, series of battery cells, it is not one chunk that fails at once). The capacity for the ions to "stay" on the desired side of the anode-cathode pair diminishes over time.
This is why batteries are advertised as maintaining x amount (usually 80%) after x cycles (usually 500) and why a device having a good Battery Management System (BMS) can be as important as how many mAH units a battery is rated as having.
As to why a plugged in battery suffers the same fate? Physics is cruel. A charge cycle is just defined as using an amount equal to 100% of your battery. Nothing says it has to be all at once.
A plugged-in lithium-ion battery still undergoes wear because it experiences minor discharges and recharges, contributing to charge cycles. Heat from constant charging and chemical aging also degrade the battery over time, leading to shorter battery life when eventually used unplugged.