If your EAD is valid on the day your H-1B cap-subject application is filed, you are protected by the "Cap Gap" rule. You can stay and work in the US until October 1, when your H-1B status is due to begin. If your OPT expires before your H-1B petition is filed then it depends on several factors.
Generally, OPT holders have a 60-day grace period after their EAD expires. During this time, they can stay in the US, and they can file an application to change to another immigration status. If you are within the 60 day grace period and are still present in the US, your employer can file a change of status on your behalf and this will allow you to stay in the US while your application is pending, but will not give you work authorization. Therefore, you must stop working for your employer until October 1, when your H-1B status period begins.
If your application is selected in the lottery and your change of status request is approved, you can stay in the US until October 1 when your H1B status begins. If your application is not selected in the lottery, or is selected but then denied, you would need to leave immediately.
This is a general rule and may not apply to your specific situation. Please consult with an immigration attorney for your particular case.