The very short version is
After the Iranian Hostage Crisis, the US placed an arms embargo on Iran so they legally couldn't sell arms to Iran
Reagan wanted to fund the Contra rebels in Nicaragua, however Congress had prevented him from directly funding them so they had to find some way of doing that off the books.
So since Iran wanted to buy weapons, they sold them some weapons so they could turn around and use that money to fund the Contras and circumvent Congress.
They also tried to justify it because they were hoping Iran would influence Hezbollah to release hostages they had taken in Lebanon (which kind of worked, the hostages were released, but Hezbollah pretty much immediately took more hostages, so not really)
This glosses over a lot of details of course.