Normally you can withdraw contributions without penalty, immediately after the funds clear (so, say, within a few days). If for some reason you do have to withdraw funds, if you wish to, you also have 60 days to put the funds back in without having it count towards the annual contribution max, and you can do this once per calendar year. Note it would normally be pointless to deposit the funds if you knew for sure you were going to withdraw it shortly after.
That being said, hypothetically, if you can't currently afford to contribute to a Roth but believe you will be able to within a couple of months, and you are close to the cutoff (April 15ish1) to still mark the funds for last year contributions, you could deposit the funds now (however you can come up with it), withdraw the money a few days later (and put it back where you got it), and then within 60 days when your expected money is received, put the money back into your Roth and it will still count as last year's contribution. Had you waited until May or June to deposit it for the first time you wouldn't have been able to mark it as last year's contribution. Note the only advantage you gain from doing this is to enable yourself to still contribute the full max amount again this year.
More generally, because you can withdraw Roth contributions without penalty, you don't have to choose between having an emergency fund and a Roth IRA. If you don't have enough to fund both, you can safely put your emergency fund into a Roth, and if you don't have an emergency, that's great! You can leave the money there. But if you do have one, you simply withdraw it and no harm was done. It's certainly better to put the money into the Roth and have to take it out, than it is to not put the money into the Roth just in case you might need it, end up not needing it, and losing the chance to put that money in for the year.
- If you do plan to use some portion of your Roth IRA as an emergency fund, don't invest it; leave it in cash. This way there will be no risk of losing it. As you build up your Roth beyond your emergency fund needs, go ahead and invest the excess.
- If you do have to pull money out, and you manage to put it back in within 60 days, make sure you contact your bank to tell them you're putting the previous distributions back in so they can mark it as such (it's considered a rollover) and it won't count as contributions against your max allowed for the current year. (Just in case it isn't the default.)
1 "April 15ish" is used to signify that the tax-day cutoff is the first non-holiday weekday on or after April 15. This year, April 15, 2022 is a Friday but also a holiday (Emancipation Day), so the next non-holiday weekday is Monday April 18, 2022.