If you fit a 40V battery to a 20V tool, you will likely destroy one or both, except they ought to have different connections to prevent this!
Lithium batteries and the like store a lot of energy, are potentially dangerous.
They can have safety measures incorporated in them such as temperature sensing and sometimes digital communication between battery and tool. Different manufacturers can adopt different approaches, so circuits can vary, for which reason different brands are typically not interchangeable.
I urge you not to experiment by trying "incorrect" batteries in a product!
How dangerous are lithium-ion batteries? Here, we dive into the statistics and risks associated with lithium-ion batteries.
www.checkfire.co.uk
The mAh rating indicates the amount of charge the battery can store and deliver, ie typically related to how long it will give the necessary voltage before it needs recharging.
If you need more 'oomph' to do the job - drilling or cutting - you really need all of:
A more powerful motor in the tool, which may mean higher voltage
The higher voltage battery to operate the more powerful tool, and
The higher mAh rating for the battery that comes with that.