Use 2:1 leverage to turn that 20k into 40k.
When to buy:
Use Warren Buffet's famous quote: "Be fearful when others are greedy, and greedy when others are fearful." If you're reading an article about why XXX stock is great, it's already too late to get in (meme stocks might be an exception).
If it feels like you have to buy XXX stock NOW or the price will go up, it's already too late. Take a few days to mull it over. Never FOMO.
When to sell:
Set stop losses. I set a stop loss at -10%. Diamond hands is a tactic people tell others to use when they themselves want to sell.
If you are at the point where your gains are good enough that you want to take a screenshot and send it to your friend--this is a good time to sell.
EDIT: Because I realize I answered the question "How would you invest 20k in the stock market?" and not "How would you invest 20k?": I would absolutely run from real estate right now. Prices are sky high due to inflation / lumber shortage / supply. You would be buying at peak.
If I had a spare 20k I would use maybe 1k to attend a conference and learn a skill which would help me earn more money in the future. I would put 15k into the stock market using above method, or crypto. And I would use the remaining money to fund a passive income online business.