Here are some best practices you can follow to ensure that a code does not hit the governor limits.
* Bulkify your Code
* Use @future Appropriately
* Don’t make any SOQL, DML operation inside a loop
* Queue large datasets
* Use batch apex for more than 50,000 records