The rules will vary between countries, but in the UK (and probably elsewhere) it can be a grey area.
If there's an ownership clause in your employment contract, that will generally be upheld in court. But a lot depends on whether your job involves duties that could be expected to lead to invention. If it doesn't, then a case can be made that the employee is entitled the intellectual property rights, especially for anything done out of hours and not involving company resources. (Because you were never paid to invent and were not provided with the resources to do so.)
Even if your work does involve activities that could lead to invention, there's still an argument over inventions unrelated to your employment. (Because you're known to be an inventor and would be expected to invent at the weekends for your own purposes.) This is a weaker argument, but not unheard of in case history.
If a dispute ever breaks out, some things to be aware of:
1. there's no statute of limitations on patents. A claim can be made decades after the fact.
2. there's no requirement for either side to fully disclose all evidence before a trial. Either side can (and usually does) keep a few wild cards up their sleeves for use at the hearing.
3. there's no burden of proof. The ultimate decision is made by the judge (usually a senior patent examiner), and it'll be for one side or the other rather than sharing/joint ownership arrangement.
4. emails are accepted as evidence. The employer will generally dig out anything that suggests you invented for the company.
In the UK, the final hearing is held at a special branch of the High Court, and you will need a specialist barrister. You can be looking at well over £1000/hr in fees. They will probably need to consult a Queen's Councillor, and they are five figures per hour. (The hearing is actually not that formal, no wigs and gowns, but they do have a stenographer typing everything up.)
Before you get to that stage there will be a mediation session with an independent barrister (fees shared between opposing parties), and there's a general expectation that the parties will try to resolve things out of court.
Case history is cloudy on the matter, so ultimately it boils down to who convinces the judge on the day. (Actually it usually runs to two days.) There is an option to appeal the decision, but it rarely changes the verdict.
If you ever get into a dispute with a small company, then your best strategy is to stand your ground and try to reach a compromise arrangement where both sides benefit. (Always be seen to act reasonably.) Larger companies will have the money to up the ante until you fold, so you can expect an 18-month battle and £50k+ in fees.
The other (safer) option is to quit the company and wait six months before doing anything with your idea, but even then you're not completely in the clear. As they say in Holywood, "where there's a hit, there's a writ". If someone wants to take your idea off you, there's nothing to stop them trying.
(You might also want to look into intellectual property insurance companies, just in case.)